【发布时间】:2015-03-26 04:48:56
【问题描述】:
我是 C# 的新手,我正在尝试通过以下代码自动从 Outlook 发送电子邮件,它在开发环境中运行良好。即使没有打开 Outlook,我也希望它使用默认用户作为发件人。
private void EmailMessage(string recipient, string subject, string body)
{
Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem email = (Outlook.MailItem)application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
try
{
email.Subject = subject;
email.Body = body;
email.To = recipient;
((Outlook._MailItem)email).Send();
_emailConfirmation = true;
}
catch (System.Runtime.InteropServices.COMException ex)
{
Logging.LogError("Trip Email Failed", ExceptionHelper.GetInnerMostException(ex));
_emailConfirmation = false;
}
finally
{
//release the objects used to send email after message has been sent\\
if (email != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(email);
if (application != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
}
}
所有用户都分配了一个帐户并安装了带有有效防病毒软件的 Outlook。我担心的是,当它上线时,它会在创建新的前景实例或其他我没有看到的东西时失败。您认为这将与我打算在它上线后完成的工作相一致吗?
代码没有错误,但是我看到很多人说你不应该直接创建outlook.application的实例。
https://msdn.microsoft.com/en-us/library/office/bb622502.aspx
我想我可能只是偏执,因为我以前从未使用过 PIA
【问题讨论】:
-
我不确定你在问什么......它在你的开发环境中对你有用吗?如果没有,出了什么问题?
-
确切的错误是什么?
-
代码没有错误,但是我看到很多人说你不应该直接创建outlook.application的实例。 msdn.microsoft.com/en-us/library/office/bb622502.aspx 我想我可能只是偏执,因为我以前从未使用过 PIA
-
我没有在代码中看到任何奇怪的地方。请参阅sample code 了解更多信息。