0

目前,我正在尝试使用 VB.NET 发送电子邮件。现在,我添加了这段代码的引用:(我添加了占位符)

Module Module1

    Sub Main()
        ' Create an Outlook application.
        Dim oApp As Outlook._Application
        oApp = New Outlook.Application()

        ' Create a new MailItem.
        Dim oMsg As Outlook._MailItem
        oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
        oMsg.Subject = "Send Attachment Using OOM in Visual Basic .NET"
        oMsg.Body = "Hello World" & vbCr & vbCr

        ' TODO: Replace with a valid e-mail address.
        oMsg.To = "user@example.com"

        ' Add an attachment
        ' TODO: Replace with a valid attachment path.
        Dim sSource As String = "C:\Temp\Hello.txt"
        ' TODO: Replace with attachment name
        Dim sDisplayName As String = "Hello.txt"

        Dim sBodyLen As String = oMsg.Body.Length
        Dim oAttachs As Outlook.Attachments = oMsg.Attachments
        Dim oAttach As Outlook.Attachment
        oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)

        ' Send
        oMsg.Send()

        ' Clean up
        oApp = Nothing
        oMsg = Nothing
        oAttach = Nothing
        oAttachs = Nothing
    End Sub

End Module

对于所有 Outlook 项目(Outlook.Application、Outlook._MailItem、Outlook、Outlook.Attachments、Outlook.Attachment)未声明或未定义,我如何才能使引用正常工作。

提前致谢。

4

3 回答 3

1

在解决方案资源管理器中,右键单击您的项目并选择“添加引用”并向下滚动,直到看到 Microsoft.Office.Interop.Outlook 并选择该项目。然后在 VB 文件的顶部添加“Imports Microsoft.Office.Interop”。

于 2011-07-06T21:42:31.623 回答
1

添加对“Microsoft Outlook 11.0 对象库”的引用:

  1. 在项目菜单上,单击添加引用。
  2. 在 COM 选项卡上,单击 Microsoft Outlook 11.0 对象库,然后单击选择。
  3. 单击“添加引用”对话框中的“确定”以接受您的选择。如果系统提示您为您选择的库生成包装器,请单击是。

在代码中你必须添加这个:

Imports Outlook = Microsoft.Office.Interop.Outlook

在此处找到更多信息: 使用 Microsoft Office Outlook 2003 和 Visual Basic .NET 的便捷任务

但是,如果您使用的是 .NET,为什么不使用System.Net.Mail呢?

于 2011-07-06T21:39:13.403 回答
-1
Imports Microsoft.Office.Interop

'On the Project menu, click Add Reference.
'On the COM tab, Double click ->  Microsoft Outlook xx.0 Object Library

Module Module1

    Sub Main()
        ' Create an Outlook application.
        Dim oApp As Outlook._Application
        oApp = New Outlook.Application()

        ' Create a new MailItem.
        Dim oMsg As Outlook._MailItem
        oMsg = CType(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook._MailItem)
        oMsg.Subject = "Send Attachment Using OOM in Visual Basic .NET"
        oMsg.Body = "Hello World" & vbCr & vbCr

        ' TODO: Replace with a valid e-mail address.
        oMsg.To = "user@example.com"

        ' Add an attachment
        ' TODO: Replace with a valid attachment path.
        Dim sSource As String = "C:\Temp\Hello.txt"
        ' TODO: Replace with attachment name
        Dim sDisplayName As String = "Hello.txt"

        Dim sBodyLen As Integer = oMsg.Body.Length
        Dim oAttachs As Outlook.Attachments = oMsg.Attachments
        Dim oAttach As Outlook.Attachment
        oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)

        ' Send
        oMsg.Send()

        ' Clean up
        oApp = Nothing
        oMsg = Nothing
        oAttach = Nothing
        oAttachs = Nothing
    End Sub

End Module
于 2011-07-06T22:38:54.837 回答