2012-07-04 8 views
12

Desidero utilizzare le e-mail di invio tramite Outlook come descritto in here. Funziona bene finché ho già aperto Outlook. Ad esempio, se Outlook è ridotto a icona e io eseguo il mio codice, allora posso inviare una e-mail bene. Ma se Outlook è chiuso, allora ottengo un'eccezione:È possibile inviare e-mail solo tramite Outlook se Outlook è aperto

{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} 

Ecco il codice:

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

Perché non funziona?

Edit: Ecco la soluzione

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

non utilizzare Outlook. Invece, utilizzare System.Net.Mail. – SLaks

+0

Buona domanda. Sicuro che non ti abbia ancora effettuato l'accesso? – BugFinder

+0

SLaks, vorrei. Sfortunatamente sto mantenendo il codice VB6 e ho semplicemente replicato il problema in C#. –

risposta

12

Il seguente codice è affidabile lavorato per mesi per me:

  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. 

Se Outlook è stato aperto lo usa, se non la sua aprì . Naturalmente, se la tua prospettiva richiede di effettuare il login, il tuo codice non lo consentirà. Alcuni sistemi rendono difficile il login automatico.

+0

Questo ha funzionato, grazie. –

10

non mi piaceva l'idea di utilizzare Thread.Sleep per 5 secondi, quindi ho trovato un'altra soluzione, che ha funzionato per me:

Tutto ciò che serve è avere oggetto Inspector per appena creato

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

risposta è stato pubblicato nel Google groups originariamente per Outlook 2007 (ma ha funzionato per me con Outlook 2010)

+0

Thanks.Great Job :) –

+0

Come può essere d'aiuto questo oggetto Inspector? Non viene usato da nessuna parte o mi manca qualcosa? –

+0

Sembra che Outlook restituisca Inspector solo dopo che è stato inizializzato correttamente. Questo è il trucco. Non devi usarlo. – Woodman

Problemi correlati