2012-02-14 198 views
0

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時發生了什麼?我需要一種方法來從我的應用程序發送電子郵件,如果有人能幫助我:)

回答

0

我從來沒有試圖做你在做什麼,但應該不是你的臺詞:

Dim OutlookMessage As outlook.MailItem = AppOutlook.CreateItem(outlook.OlItemType.olMailItem) 
    AppOutlook = CreateObject("Outlook.Application") 

被逆轉?這樣你的OutlookMes​​sage不是基於新的實例?當然,是的,如果他們沒有安裝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) 

但是,這感覺有點不對我。我想我可能會嘗試這些解決方案之一,並給予用戶機會給你一些設置,如果不是這樣的話。

+0

我得到以下行錯誤: – user1196604 2012-02-14 12:29:51

+0

昏暗SMTP作爲新SmtpClient() – user1196604 2012-02-14 12:30:00

+0

昏暗的電子郵件作爲MAILMESSAGE新() – user1196604 2012-02-14 12:30:10