如何通过 vb.net 以编程方式使用 Outlook 捕获异常发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19954117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send email via vb.net catch exception with outlook programmatically?
提问by Narasimha
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 问题(字符串)发送电子邮件?
采纳答案by skynetsysadmin
I use this to send via outlook. Just reference the Microsoft Outlook DLL
我用它通过 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 = "[email protected]"
.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
回答by Steve
First, you should cath your errors, not let your service crash. Second, you can use the System.Net.Mailnamspace to send mail. You will want to create a MailMessage and send it through a SMTPClient. Each will give you examples.
首先,你应该引导你的错误,而不是让你的服务崩溃。其次,您可以使用System.Net.Mailnamspace 发送邮件。您需要创建一个 MailMessage 并通过 SMTPClient 发送它。每个人都会给你例子。

