• 全部
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API
  • 问答

[求救!]发送邮件程序的错误

xmjcwo 2003-12-24 11:51:24
原文件
Imports System
Imports System.Web
Imports System.Data
Imports System.Drawing
Imports System.Web.Mail
Imports System.Collections
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class FrmSendMail
Inherits System.Windows.Forms.Form
Dim message As New MailMessage()
'定义一个新的邮件消息
Dim sendTo As String
'定义存取发送目的地址
Dim sendFrom As String
'定义存取发送原址
Dim Subject As String
'定义邮件标题
Dim Attachs As MailAttachment
'定义邮件附件,注意,在这里一次只能添加一个附件
Dim proity As MailPriority
'定义邮件发送优先级别
#Region " Windows 窗体设计器生成的代码 "
。。。。。。。。。。
#End Region

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Me.ComboBox1.SelectedIndex = 0
sendTo = Me.TextBox1.Text
sendFrom = Me.TextBox2.Text
If sendTo = "" Then
MessageBox.Show("没有邮件目的地址,请重新输入," & _
"并注意格式的正确性。", "错误", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
'获取并判断目的地址是否为空
message.To = sendTo
message.From = sendFrom
'设置邮件的发送目的地址和原址
If Me.RadioButton1.Checked = True Then
message.BodyFormat = MailFormat.Text
End If
If Me.RadioButton2.Checked = True Then
message.BodyFormat = MailFormat.Text
End If
'选择邮件发送的格式
message.BodyEncoding = System.Text.Encoding.UTF8
'选择邮件发送的文本编码
Subject = Me.TextBox3.Text
Dim result As DialogResult
If Subject = "" Then
result = MessageBox.Show("该封邮件没有添加标题,不要标题吗?", _
"提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
End If
'判断该邮件是否具有标题
If result = DialogResult.No Then Exit Sub
'如果选择要添加标题,则退出过程等待添加标题
message.Subject = Subject
'设置邮件发送标题
message.Body = Me.RichTextBox1.Text
'获取邮件发送的内容
If Me.TextBox4.Text = "" Then
Else
Attachs = New MailAttachment(Me.TextBox4.Text)
message.Attachments.Add(Attachs)
'判断是否具有附件,如果有则连带附件发送
End If
If Me.TextBox5.Text = "" Then
Else
message.Cc = Me.TextBox5.Text
End If
'判断是否有抄送地址
SmtpMail.SmtpServer = "218.5.*.*"
Try
SmtpMail.Send(message)
Catch
MessageBox.Show(Err.Description, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
'发送邮件
Catch
MessageBox.Show("邮件没有发送成功!原因是:" & Err.Description, _
"错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
'获取可能发生意外的错误并通知用户
Select Case Me.ComboBox1.SelectedIndex
Case 0
proity = MailPriority.Normal
Case 1
proity = MailPriority.High
Case 2
proity = MailPriority.Low
End Select
message.Priority = proity
'选择并设置邮件发送的优先级
MessageBox.Show("邮件已经成功发送完毕。", "提示", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
'
End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
With Me.OpenFileDialog1
.Title = "打开"
.Filter = "所有文件(*.*)|*.*"
.ShowDialog()
Me.TextBox4.Text = .FileName
End With
'获取邮件附件的文件名和完整路径
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
'退出程序
End Sub

End Class


发送时出错“未能访问CDO.Message对象”,请问个大虾到底错在哪里啊,谢谢!
...全文
42 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zkhj 2004-02-11
以下这段代码,是完整的,只要修改SMTP设置就可以了

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Cursor.Current = Cursors.WaitCursor
Dim email As New MailMessage

email.BodyFormat = MailFormat.Html 'Html格式的邮件 如何加入有图片的邮件正文?
email.From = "from@changansuzuki.com"
email.To = "to@changansuzuki.com" '以分号分隔多个收件人

email.Subject = "SubjectText"

email.Body = "<html><h1>中国</h1><img src='http://bjpic.163.net/images/cm25_style_tom/button-clean.gif'></html>"



email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")

email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "from@changansuzuki.com") 'SMTP 用户

email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "88888888") 'SMTP 密码

email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.163.net") 'SMTP服务器

System.Web.Mail.SmtpMail.Send(email)
Cursor.Current = Cursors.Default
End Sub
回复
xmjcwo 2003-12-24
这不是一样吗
回复
super119 2003-12-24
Imports System.Web.Mail

Dim mailmsg As New MailMessage()
SmtpMail.SmtpServer = "yourmailserver"
mailmsg.From = txtFrom.Text
mailmsg.To = txtTo.Text
mailmsg.Priority = MailPriority.High
If txtAttachment.Text <> "" Then
mailmsg.Attachments.Add(New MailAttachment(txtAttachment.Text))
End If
mailmsg.Subject = txtSubject.Text
mailmsg.BodyFormat = MailFormat.Html
Dim msgstr As String
Dim tt As Char()
Try
msgstr = txtMsg.Text
msgstr = Replace(msgstr, Chr(13), "<BR>")
msgstr = Replace(msgstr, Chr(10), "")
mailmsg.Body = msgstr
SmtpMail.Send(mailmsg)
Button1.Enabled = True
MsgBox("发送完成!")
Catch
MsgBox("发送失败!")
Button1.Enabled = True
End Try
回复
相关推荐
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2003-12-24 11:51
社区公告
暂无公告