简体   繁体   English

如何通过vb.net发送电子邮件以编程方式捕获Outlook的异常?

[英]How to send email via vb.net catch exception with outlook programmatically?

I am working on vb.net service application and application hosting in to the server running daily, sometimes service is stopped because exception is raising. 我正在研究vb.net服务应用程序,并且正在将应用程序托管到每天运行的服务器中,有时由于出现异常而停止了服务。

How to send email via vb.net issue (String) with outlook programmatically? 如何通过Outlook以编程方式通过vb.net问题(字符串)发送电子邮件?

I use this to send via outlook. 我用它来通过Outlook发送。 Just reference the Microsoft Outlook DLL 只需引用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 has a lot of info on this as well. MSDN上也有很多信息。

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

First, you should cath your errors, not let your service crash. 首先,您应该注意错误,而不要让服务崩溃。 Second, you can use the System.Net.Mail namspace to send mail. 其次,可以使用System.Net.Mail命名空间发送邮件。 You will want to create a MailMessage and send it through a SMTPClient. 您将要创建一个MailMessage并通过SMTPClient发送它。 Each will give you examples. 每个都会给你例子。

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

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