2014-04-03 57 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

很高兴我能帮到你。 –