2014-04-03 93 views
0

我想在用戶提交表單時發送確認電子郵件。我有smtp privlage問題,所以我想通過Outlook來做到這一點。當Outlook未打開時,代碼正常工作enter link description here,但只是掛起。任何想法如何解決這個或周圍的方式?當Outlook打開時,Microsoft.Office.Interop.Outlook確認電子郵件不起作用

try 
{ 

// Create the Outlook application. 
Outlook.Application oApp = new Outlook.Application(); 

// Create a new mail item. 
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
// Set HTMLBody. 
//add the body of the email 
oMsg.HTMLBody = "Hello, Jawed your message body will go here!!"; 

//Subject line 
oMsg.Subject = "Your Subject will go here."; 

// Add a recipient. 
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; 
// Change the recipient in the next line if necessary. 
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("[email protected]"); 
oRecip.Resolve(); 

// Send. 
((Outlook._MailItem)oMsg).Send(); 


// Clean up. 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oMsg); 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oRecip); 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oRecips); 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp); 
}//end of try block 

catch (Exception ex) 
{ 
}//end of catch 
+0

您是否嘗試過檢查,看看是否Outlook的現有實例是開放的,使用它,如果有一個,那麼如果沒有一個創建新實例? –

+0

我的合約上有一個實例打開。我不知道我將如何使用該實例 – joetinger

回答

1

嘗試檢查是否有已經打開的Outlook實例,如果有人使用它來發送郵件。如果沒有,則創建新的實例。

(未經測試)這應該讓你開始:

Outlook.Application oApp; 
try { 
    oApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application"); 
} 
catch() { 
    oApp = new Outlook.Application(); 
} 

// use oApp to send the stuff, it will either be the existing instance or a new instance 
+0

謝謝你的工作。我必須以管理員身份運行Outlook才能使其運行http://stackoverflow.com/questions/15228845/how-to-connect-to-a-running-instance-of-outlook-from-c-sharp感謝您的幫助! – joetinger

+0

很高興我能幫到你。 –