繁体   English   中英

Microsoft Visual Studio 中的引用不起作用

[英]References in Microsoft Visual Studio not working

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

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

How can I get the references to work, for all of the Outlook items (Outlook.Application, Outlook._MailItem, Outlook, Outlook.Attachments, Outlook.Attachment) are either undeclared or undefined.

提前致谢。

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

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

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

Imports Outlook = Microsoft.Office.Interop.Outlook

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM