我需要在端口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);