通过Outlook中的Outlook发送电子邮件时出错?
问题描述:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in myprogram.exe通过Outlook中的Outlook发送电子邮件时出错?
Additional information: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
下面的代码是什么原因造成的错误:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim AppOutlook As New outlook.Application
Dim OutlookMessage As outlook.MailItem = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
AppOutlook = CreateObject("Outlook.Application")
Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
Recipents.Add("[email protected]")
OutlookMessage.Subject = "Sending through Outlook"
OutlookMessage.Body = "Testing outlook Mail"
OutlookMessage.Send()
OutlookMessage = Nothing
AppOutlook = Nothing
End Sub
的错误是在第7行中,它说:
Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
如果这不是太复杂,是有办法这样做没有前景?因为当用户没有安装outlook时发生了什么?我需要一种方法来从我的应用程序发送电子邮件,如果有人能帮助我:)
答
我从来没有试图做你在做什么,但应该不是你的台词:
Dim OutlookMessage As outlook.MailItem = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
AppOutlook = CreateObject("Outlook.Application")
被逆转?这样你的OutlookMessage不是基于新的实例?当然,是的,如果他们没有安装outlook,你会遇到这个问题。
我认为从您的用户那里获取电子邮件设置信息的时间可能会比较困难,以一种始终有效的方式发送电子邮件。考虑到端口25阻塞和SMTP过滤等。即使您使用了用户为控制面板中列出的邮件输入的设置,现在很多人都使用基于Web的电子邮件,现在我无法想象这会起作用。我想你可以使用你自己的电子邮件帐户,你知道会发送。
Dim Smtp As New SmtpClient()
Smtp.Credentials = New Net.NetworkCredential("[email protected]", "password")
Smtp.EnableSsl = True
Smtp.Port = 587
Smtp.Host = "smtp.gmail.com"
Dim Email As New MailMessage()
Email.From = New MailAddress("[email protected]")
Email.To.Add("[email protected]")
Email.Subject = "Test Mail"
Email.Body = "SMTP server test"
Smtp.Send(Email)
但是,这感觉有点不对我。我想我可能会尝试这些解决方案之一,并给予用户机会给你一些设置,如果不是这样的话。
我得到以下行错误: – user1196604 2012-02-14 12:29:51
昏暗SMTP作为新SmtpClient() – user1196604 2012-02-14 12:30:00
昏暗的电子邮件作为MAILMESSAGE新() – user1196604 2012-02-14 12:30:10