2012-07-04 4 views
12

here과 같이 Outlook을 통해 전자 메일을 보내고 싶습니다. Outlook을 이미 연 결한 한 제대로 작동합니다. 예를 들어 Outlook을 최소화하고 코드를 실행하면 전자 메일을 보낼 수 있습니다. Outlook을 닫을 경우에, 나는 예외를 얻을 : 코드Outlook이 열려 있으면 Outlook을 통해서만 전자 메일을 보낼 수 있습니다.

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

입니다 :

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

하지 않는 이유는이 작품?

편집 : 전망이 열려있는 경우는 그것을 개방하지 않을 경우

  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. 

그것, 그것을 사용 : 여기

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을 사용하지 마십시오했다. 대신 System.Net.Mail을 사용하십시오. – SLaks

+0

좋은 질문입니다. 물론 아직 로그인하지 않았습니까? – BugFinder

+0

SLaks, 나는 소원한다. 불행히도 VB6 코드를 유지하고 C#에서 문제를 복제했습니다. –

답변

12

은 다음 코드는 확실하게 나를 위해 개월 동안 일했다 해결책을이다 . 물론, 당신의 전망이 당신에게 로그인을 요구한다면, 당신의 코드는 그것을 허용하지 않을 것입니다. 일부 시스템에서는 자동 로그인을 어렵게 만듭니다.

+0

그게 고마워. 고마워. –

10

나는 5 초에 Thread.sleep를 사용하는 아이디어를 좋아하지 않았다, 그래서 나는 나를 위해 일한 다른 솔루션을 발견했습니다

당신이 필요로하는 모든이 새로 생성을위한 검사기 객체를 얻을입니다

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

대답은 원래 아웃룩 2007 Google groups에 게시 (하지만 아웃룩 2010과 나를 위해 일한)

+0

감사합니다. 훌륭한 직업 :) –

+0

어떻게 그 검사관이 도움을 주나요? 어디서나 사용되지 않았거나 뭔가 빠져 있었습니까? –

+0

Outlook은 제대로 초기화 된 후에 만 ​​Inspector를 반환합니다. 그게 속임수 야. 당신은 그것을 사용할 필요가 없습니다. – Woodman

관련 문제