2012-07-04 62 views
12

Tôi muốn sử dụng gửi email qua Outlook như được mô tả here. Nó hoạt động tốt miễn là tôi đã mở Outlook. Vì vậy, ví dụ nếu Outlook được giảm thiểu và tôi thực thi mã của tôi, sau đó tôi có thể gửi một email tốt. Nhưng nếu Outlook được đóng lại, sau đó tôi nhận được một ngoại lệ:Chỉ có thể gửi email qua Outlook nếu Outlook đang mở

{System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT)) 
    at Microsoft.Office.Interop.Outlook._MailItem.get_Recipients() 
    at OutlookExample.Form1.btnSendEmail_Click(Object sender, EventArgs e) in C:\Users\abc\Documents\Visual Studio 2008\Projects\OutlookExample\OutlookExample\Form1.cs:line 28} 

Đây là mã:

using Outlook = Microsoft.Office.Interop.Outlook; 

... 

private void btnSendEmail_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     Outlook.Application oApp = new Outlook.Application(); 
     Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
      oMsg.HTMLBody = "Hello, here is your message!"; 
      oMsg.Subject = "This is a test message"; 
      Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; 
      Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("[email protected]"); 
      oRecip.Resolve(); 
      oMsg.Send(); 
      oRecip = null; 
      oRecips = null; 
      oMsg = null; 
      oApp = null; 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 

Tại sao không làm việc này?

Edit: Đây là giải pháp

using Outlook = Microsoft.Office.Interop.Outlook; 

... 

private void btnSendEmail_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     Outlook.Application oApp = new Outlook.Application(); 

     // These 3 lines solved the problem 
     Outlook.NameSpace ns = oApp.GetNamespace("MAPI"); 
     Outlook.MAPIFolder f = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); 
     System.Threading.Thread.Sleep(5000); // test 

     Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
      oMsg.HTMLBody = "Hello, here is your message!"; 
      oMsg.Subject = "This is a test message"; 
      Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; 
      Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("[email protected]"); 
      oRecip.Resolve(); 
      oMsg.Send(); 
      oRecip = null; 
      oRecips = null; 
      oMsg = null; 
      oApp = null; 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 
+5

Không sử dụng Outlook. Thay vào đó, hãy sử dụng System.Net.Mail. – SLaks

+0

Câu hỏi hay. Chắc chắn chưa đăng nhập của bạn? – BugFinder

+0

Slaks, tôi ước. Thật không may tôi đang duy trì mã VB6 và chỉ nhân rộng các vấn đề trong C#. –

Trả lời

12

Các mã sau đây đã đáng tin cậy làm việc trong nhiều tháng cho tôi:

  app = new Microsoft.Office.Interop.Outlook.Application(); 
      Microsoft.Office.Interop.Outlook.NameSpace ns = app.GetNamespace("MAPI"); 
      f = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox); 
      Thread.Sleep(5000); // a bit of startup grace time. 

nếu triển vọng đã được mở nó sử dụng nó, nếu không mở của nó . Tất nhiên, nếu triển vọng của bạn yêu cầu bạn đăng nhập, mã của bạn sẽ không cho phép điều đó. Một số hệ thống khiến bạn khó đăng nhập tự động.

+0

Điều đó đã làm việc, cảm ơn. –

10

Tôi không thích ý tưởng sử dụng Thread.Sleep trong 5 giây, vì vậy tôi đã tìm thấy một giải pháp, mà làm việc cho tôi:

Tất cả bạn cần là có được đối tượng thanh tra cho vừa tạo

Outlook.Application oApp = new Outlook.Application(); 
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
Outlook.Inspector oInspector = oMsg.GetInspector; 

Câu trả lời được xuất bản trong Google groups ban đầu cho Outlook 2007 (nhưng nó đã hoạt động cho tôi với Outlook 2010)

+0

Thanks.Great Job :) –

+0

Tuy nhiên, đối tượng thanh tra đó giúp đỡ như thế nào? Nó không được sử dụng bất cứ nơi nào hoặc tôi thiếu một cái gì đó? –

+0

Có vẻ như Outlook chỉ trả về Thanh tra sau khi nó được khởi tạo đúng cách. Đó là mẹo. Bạn không phải sử dụng nó. – Woodman

Các vấn đề liên quan