簡體   English   中英

打開Outlook時,Microsoft.Office.Interop.Outlook確認電子郵件不起作用

[英]Microsoft.Office.Interop.Outlook Confirmation email doesn't work when Outlook is open

我正在嘗試在用戶提交表單時發送確認電子郵件。 我在smtp上遇到特權問題,因此我正在嘗試通過Outlook進行。 如果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@address.com");
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

嘗試檢查是否有一個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM