0

我需要使用端口 465 上的外部 smtp 发送电子邮件。我了解到我不能使用 System.Net.Mail.SmtpClient 因为它不支持隐式 SSL 所以我更改了我的工作代码(它是引用其他没有 ssl 的邮件服务器)使用 System.Web.Mail.SmtpMail。它在 VS 2012 环境中运行良好,但是当我尝试在 IIS 7 上运行它时,我得到“传输无法连接到服务器”。我在想这是一些与安全相关的问题,但无法弄清楚。

我的代码:

System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", WebConfigurationManager.AppSettings["SmtpServer"]);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", WebConfigurationManager.AppSettings["SmtpPort"]);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", WebConfigurationManager.AppSettings["SmtpUser"]);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", WebConfigurationManager.AppSettings["SmtpPw"]);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
myMail.From = WebConfigurationManager.AppSettings["EmailAddress"].ToString();
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;
myMail.Body = pBody;
if (pAttachmentPath.Trim() != "")
{
   MailAttachment MyAttachment = new MailAttachment(pAttachmentPath);
   myMail.Attachments.Add(MyAttachment);
   myMail.Priority = System.Web.Mail.MailPriority.Normal;
}
if (DeliveryNotificationEmail!="")
{
    myMail.Headers.Add("Disposition-Notification-To", DeliveryNotificationEmail);
}
System.Web.Mail.SmtpMail.SmtpServer = WebConfigurationManager.AppSettings["SmtpServer"] + ":" + WebConfigurationManager.AppSettings["SmtpPort"];
System.Web.Mail.SmtpMail.Send(myMail);
4

0 回答 0