2

我正在开发 vb.net 服务应用程序和托管到每天运行的服务器的应用程序,有时服务会因为异常引发而停止。

如何以编程方式通过 Outlook 的 vb.net 问题(字符串)发送电子邮件?

4

2 回答 2

2

我用它通过outlook发送。只需参考 Microsoft Outlook DLL

    Private Shared Sub SendMail(pMessage As String)

        Dim objMissing As Object = Type.Missing
        Dim objOutlook As Outlook.Application = Nothing
        Dim objNS As Outlook.NameSpace = Nothing
        Dim objMail As Outlook.MailItem = Nothing

        Try
            ' Start Microsoft Outlook and log on with your profile.
            ' Create an Outlook application.
            objOutlook = New Outlook.Application()

            ' Get the namespace
            objNS = objOutlook.GetNamespace("MAPI")

            ' Log on by using a dialog box to choose the profile.
            objNS.Logon(objMissing, objMissing, False, False)

            ' create an email message
            objMail = CType(objOutlook.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)

            ' Set the properties of the email.
            With objMail
                .Subject = "Subject Line"
                .To = "emailaddress@domain.com"
                .Body = pMessage
                .Display(True)
            End With

        Catch ex As Exception

        Finally
            ' Manually clean up the explicit unmanaged Outlook COM resources by  
            ' calling Marshal.FinalReleaseComObject on all accessor objects. 
            ' See http://support.microsoft.com/kb/317109.

            If Not objMail Is Nothing Then
                Marshal.FinalReleaseComObject(objMail)
                objMail = Nothing

            End If

            If Not objNS Is Nothing Then
                Marshal.FinalReleaseComObject(objNS)
                objNS = Nothing

            End If
            If Not objOutlook Is Nothing Then
                Marshal.FinalReleaseComObject(objOutlook)
                objOutlook = Nothing

            End If

        End Try

    End Sub

MSDN 也有很多这方面的信息。

http://msdn.microsoft.com/en-us/library/office/ff865816.aspx

于 2013-11-13T16:19:20.103 回答
1

首先,你应该纠正你的错误,而不是让你的服务崩溃。其次,您可以使用System.Net.Mail命名空间来发送邮件。您将要创建一个 MailMessage 并通过 SMTPClient 发送它。每个都会给你例子。

于 2013-11-13T15:08:37.040 回答