在C#中发送电子邮件

10

我正在使用 .NET 3.5,并且想要自动发送邮件。目前我正在使用以下代码:

Microsoft.Office.Interop.Outlook.MailItem mailMsg = 
    (Microsoft.Office.Interop.Outlook.MailItem)outlookApplication.CreateItem(
     Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailMsg.To = recipient;
mailMsg.Subject = subject;
mailMsg.Body = body;
mailMsg.Send();

然而,我发现了几篇文章,似乎暗示我应该使用以下方法:

System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage();
mailmsg.To = recipient;
mailmsg.Subject = subject;
mailmsg.Body = body;

有人能告诉我这两个命名空间之间的区别以及为什么你可能想要使用其中一个而不是另一个吗?

11个回答

1

你需要在任何时候都使用第二个选项。它是纯粹的.NET。

如果你使用第一个选项,我猜测Outlook应该已经安装在那台机器上了。当你部署时,如果服务器上没有安装MS Office,你将会遇到问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接