解决“未能访问CDO.Message对象”提示问题

好不容易照着书上介绍的方法想编一个邮件发送程序

却也遇到了困难。搜尽了网上几乎所有的办法。

终于找到一个不算完满的解决办法,也应了那句话,世上无难事,只怕有心人。

也说明世界上没有十全十美的事!

我已经安装了微软件的IIS,想利用它的SMTP来发邮件,却怎么也行不通

无奈之下只好用网上其它人介绍的一种方法来做了,希望与各位共勉。

完整的代码如下:

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Web
Imports System.Web.Mail

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents MessageTextBox As System.Windows.Forms.RichTextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents FromTextBox As System.Windows.Forms.TextBox
    Friend WithEvents ToTextBox As System.Windows.Forms.TextBox
    Friend WithEvents CCTextBox As System.Windows.Forms.TextBox
    Friend WithEvents BCCTextBox As System.Windows.Forms.TextBox
    Friend WithEvents SubjectTextBox As System.Windows.Forms.TextBox
    Friend WithEvents AttachmentTextBox As System.Windows.Forms.TextBox
    Friend WithEvents BrowseButton As System.Windows.Forms.Button
    Friend WithEvents SendButton As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.MessageTextBox = New System.Windows.Forms.RichTextBox
        Me.SendButton = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.BrowseButton = New System.Windows.Forms.Button
        Me.FromTextBox = New System.Windows.Forms.TextBox
        Me.ToTextBox = New System.Windows.Forms.TextBox
        Me.CCTextBox = New System.Windows.Forms.TextBox
        Me.BCCTextBox = New System.Windows.Forms.TextBox
        Me.SubjectTextBox = New System.Windows.Forms.TextBox
        Me.AttachmentTextBox = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(32, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(54, 17)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "发信人:"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(32, 48)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(54, 17)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "收信人:"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(32, 88)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(42, 17)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "抄送:"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(32, 128)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(42, 17)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "暗送:"
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(32, 168)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(42, 17)
        Me.Label5.TabIndex = 4
        Me.Label5.Text = "主题:"
        '
        'Label6
        '
        Me.Label6.AutoSize = True
        Me.Label6.Location = New System.Drawing.Point(32, 208)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(42, 17)
        Me.Label6.TabIndex = 5
        Me.Label6.Text = "附件:"
        '
        'MessageTextBox
        '
        Me.MessageTextBox.AcceptsTab = True
        Me.MessageTextBox.Location = New System.Drawing.Point(32, 240)
        Me.MessageTextBox.Name = "MessageTextBox"
        Me.MessageTextBox.Size = New System.Drawing.Size(384, 160)
        Me.MessageTextBox.TabIndex = 6
        Me.MessageTextBox.Text = ""
        '
        'SendButton
        '
        Me.SendButton.Location = New System.Drawing.Point(72, 416)
        Me.SendButton.Name = "SendButton"
        Me.SendButton.Size = New System.Drawing.Size(88, 32)
        Me.SendButton.TabIndex = 7
        Me.SendButton.Text = "发送邮件"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(248, 416)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 32)
        Me.Button2.TabIndex = 8
        Me.Button2.Text = "退出程序"
        '
        'BrowseButton
        '
        Me.BrowseButton.Location = New System.Drawing.Point(328, 192)
        Me.BrowseButton.Name = "BrowseButton"
        Me.BrowseButton.Size = New System.Drawing.Size(88, 32)
        Me.BrowseButton.TabIndex = 9
        Me.BrowseButton.Text = "浏览文件"
        '
        'FromTextBox
        '
        Me.FromTextBox.Location = New System.Drawing.Point(80, 8)
        Me.FromTextBox.Name = "FromTextBox"
        Me.FromTextBox.Size = New System.Drawing.Size(296, 21)
        Me.FromTextBox.TabIndex = 10
        Me.FromTextBox.Text = ""
        '
        'ToTextBox
        '
        Me.ToTextBox.Location = New System.Drawing.Point(80, 40)
        Me.ToTextBox.Name = "ToTextBox"
        Me.ToTextBox.Size = New System.Drawing.Size(296, 21)
        Me.ToTextBox.TabIndex = 11
        Me.ToTextBox.Text = ""
        '
        'CCTextBox
        '
        Me.CCTextBox.Location = New System.Drawing.Point(80, 80)
        Me.CCTextBox.Name = "CCTextBox"
        Me.CCTextBox.Size = New System.Drawing.Size(296, 21)
        Me.CCTextBox.TabIndex = 12
        Me.CCTextBox.Text = ""
        '
        'BCCTextBox
        '
        Me.BCCTextBox.Location = New System.Drawing.Point(80, 120)
        Me.BCCTextBox.Name = "BCCTextBox"
        Me.BCCTextBox.Size = New System.Drawing.Size(296, 21)
        Me.BCCTextBox.TabIndex = 13
        Me.BCCTextBox.Text = ""
        '
        'SubjectTextBox
        '
        Me.SubjectTextBox.Location = New System.Drawing.Point(80, 160)
        Me.SubjectTextBox.Name = "SubjectTextBox"
        Me.SubjectTextBox.Size = New System.Drawing.Size(296, 21)
        Me.SubjectTextBox.TabIndex = 14
        Me.SubjectTextBox.Text = ""
        '
        'AttachmentTextBox
        '
        Me.AttachmentTextBox.Location = New System.Drawing.Point(80, 200)
        Me.AttachmentTextBox.Name = "AttachmentTextBox"
        Me.AttachmentTextBox.ReadOnly = True
        Me.AttachmentTextBox.Size = New System.Drawing.Size(176, 21)
        Me.AttachmentTextBox.TabIndex = 15
        Me.AttachmentTextBox.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(440, 461)
        Me.Controls.Add(Me.AttachmentTextBox)
        Me.Controls.Add(Me.SubjectTextBox)
        Me.Controls.Add(Me.BCCTextBox)
        Me.Controls.Add(Me.CCTextBox)
        Me.Controls.Add(Me.ToTextBox)
        Me.Controls.Add(Me.FromTextBox)
        Me.Controls.Add(Me.BrowseButton)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.SendButton)
        Me.Controls.Add(Me.MessageTextBox)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "发送邮件"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub BrowseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseButton.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            AttachmentTextBox.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub SendButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendButton.Click
        If (Me.FromTextBox.Text = "") Then
            MsgBox("发件人的地址不能为空!", MsgBoxStyle.Exclamation, "邮件发送程序")
            Exit Sub
        End If
        If (Me.ToTextBox.Text = "") Then
            MsgBox("收件人的地址不能为空!", MsgBoxStyle.Exclamation, "邮件发送程序")
            Exit Sub
        End If
        If (Me.SubjectTextBox.Text = "") Then
            MsgBox("主题不能为空!", MsgBoxStyle.Exclamation, "邮件发送程序")
            Exit Sub
        End If
        If (Me.MessageTextBox.Text = "") Then
            MsgBox("邮件内容不能为空!", MsgBoxStyle.Exclamation, "邮件发送程序")
            Exit Sub
        End If
        Dim aMessage As New MailMessage
        aMessage.From = FromTextBox.Text
        aMessage.To = ToTextBox.Text
        aMessage.Cc = CCTextBox.Text
        aMessage.Bcc = BCCTextBox.Text
        aMessage.Subject = SubjectTextBox.Text
        aMessage.Body = MessageTextBox.Text
        If AttachmentTextBox.Text.Length > 0 Then
            aMessage.Attachments.Add(New MailAttachment(AttachmentTextBox.Text, MailEncoding.Base64))
        End If
        SmtpMail.SmtpServer = "smtp.163.com"
        aMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") ' //basic authentication
        aMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "nmanszx") ' //username
        aMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456") ' //password
        Try
            SmtpMail.Send(aMessage)
        Catch
            MsgBox("邮件发送失败。" & vbCrLf & Err.Description & "请检查发件人地址或收件人地址是否正确。", MsgBoxStyle.Critical _
            , "邮件发送程序")
            Exit Sub
        End Try
        MessageBox.Show("邮件成功发送到" & ToTextBox.Text, "邮件发送程序")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox("感谢你使用此邮件发送系统!", MsgBoxStyle.Information, "邮件发送程序")
        Application.Exit()
    End Sub
End Class

以上代码的缺点就是发件人只能填自己设定的163网站用户。但收件人还是没有限制。算是小小达到要求了吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB CDO (Collaboration Data Objects) 是一种用于在VB语言中接收邮件的编程接口。使用VB CDO可以方便地连接到邮件服务器并接收电子邮件。 首先,我们需要在VB项目中添加CDO库的引用。它可以在“项目”菜单中的“引用”选项中找到。我们需要选择"CDO for Windows Library"以使用CDO的功能。 接下来,我们需要在VB代码中实例化一个CDO对象,用于连接到邮件服务器。使用以下代码可以创建一个CDO.Session对象: ``` Dim objSession As CDO.Session Set objSession = CreateObject("MAPI.Session") ``` 接下来,我们需要通过设置Session对象的属性来配置连接信息。例如,我们可以设置邮件服务器的名称和端口号,设置用户名和密码等。以下是一个设置连接信息的示例代码: ``` objSession.Logon , , False, True, , False objSession.CMAPIFlags = CdoCMAPILogon objSession.MapiProfile = "username" ``` 然后,我们可以使用Session对象的GetDefaultFolder方法来获取收件箱(Inbox)文件夹的引用。以下是一个获取收件箱的示例代码: ``` Dim objFolder As CDO.IFolder Set objFolder = objSession.GetDefaultFolder(CdoDefaultFolderInbox) ``` 最后,我们可以遍历收件箱中的所有邮件,读取邮件的主题、发件人、收件人等信息。以下是一个读取邮件信息的示例代码: ``` Dim objMessage As CDO.Message For Each objMessage In objFolder.Messages MsgBox "主题:" & objMessage.Subject & vbCrLf & "发件人:" & objMessage.SenderName Next objMessage ``` 以上就是使用VB CDO接收邮件的基本步骤。通过实例化CDO对象、配置连接信息、获取文件夹引用以及遍历邮件来读取信息,我们可以轻松地在VB中接收邮件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值