2012-07-04 22 views
12

here açıklandığı gibi Outlook aracılığıyla e-posta göndermek istiyorum. Outlook'u zaten açtığım sürece iyi çalışıyor. Örneğin, örneğin Outlook küçültülmüş ve kodumu çalıştırırsam, bir e-posta gönderebilirim. Outlook kapalı Ama eğer bir istisna olsun: KodOutlook açıldığında Outlook'u yalnızca e-posta gönderebilir

İşte
{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} 

geçerli:

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()); 
    } 
} 

neden değil bu iş?

Düzenleme: outlook açık olsaydı onun açtı değilse

  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. 

zahmete kullanır: Burada

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

Outlook kullanmayın edildi. Bunun yerine, System.Net.Mail'i kullanın. – SLaks

+0

İyi soru. Henüz giriş yapmadığından emin misin? – BugFinder

+0

SLaks, Keşke. Ne yazık ki ben VB6 kodu koruyorum ve sadece C# içinde sorunu çoğaltılır. –

cevap

12

Aşağıdaki kod güvenilir benim için aylarca çalıştı çözümdür . Tabii ki, görünümünüz giriş yapmanızı gerektiriyorsa, kodunuz buna izin vermez. Bazı sistemler otomatik giriş yapmanızı zorlaştırır.

+0

Bu işe yaradı, teşekkürler. –

10

Ben 5 saniye Thread.Sleep kullanmak fikri beğenmedi, bu yüzden benim için çalışan başka bir çözüm, buldum:

Tek ihtiyacınız Yeni oluşturulan için Müfettiş nesne olsun

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

Cevap aslen Outlook 2007 için Google groups yayınlanan (ama Outlook 2010 ile benim için çalıştı)

+0

Thanks.Great Job :) –

+0

Bu denetçi nesnesi nasıl yardımcı olur? Hiçbir yerde kullanılmıyor mu yoksa bir şey mi özlüyorum? –

+0

Görünüşe göre Outlook, Müfettişe yalnızca düzgün bir şekilde başlatıldıktan sonra geri döner. Bu hile. Kullanmak zorunda değilsin. – Woodman

İlgili konular