No Office app (including Outlook) can run in a service (such as IIS). Your options are Extended MAPI (C++ or Delphi only), Redemption (which wraps Extended MAPI and can be accessed from any language, including C# - I am its author), EWS (Exchange only) or straight SMTP.
Sending Email through Outlook not working on IIS
题
While running in debug my code runs fine and emails appropriately through Outlook however when I throw it on IIS (same machine and dev) the emails aren't going through. Any ideas why?
Current code
try
{
//creates outlook app
Outlook.Application oApp;
//checks to see if outlook is already running, if not create a new instance of it
try {
oApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
}
catch (Exception ex) {
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 = "This is an email to notify you that there is a new Experimental MEO added";
//Subject line
oMsg.Subject = "NEW MEO EXPERIMENTAL ADDED.";
// 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
解决方案
不隶属于 StackOverflow