.NET 4 在发送电子邮件时是否有任何问题?我有一个 .NET 3.5 解决方案,它正在工作,然后将该解决方案迁移到 .NET 4,但它不起作用;没有改变!
注意:我得到了这个例外:
System.Net.Mail.SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。
在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport的 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 了解更多信息System.Net.Mail.SmtpClient.Send( MailMessage
message)
at ...
这是我的代码:
public static void SendEmail(
this Email email)
{
if (email.MailingSettings == null) throw new ArgumentNullException("email.MailingSettings", "specify email.MailingSettings");
var settings = email.MailingSettings;
if (string.IsNullOrEmpty(email.Sender)) throw new ArgumentException("email.Sender", "specify a sender");
if (email.Recipients == null) throw new ArgumentNullException("email.Recipients", "specify at least a recipient");
if (email.Recipients.Length == 0) throw new ArgumentNullException("email.Recipients", "specify at least a recipient");
if (string.IsNullOrEmpty(settings.SMTPHost)) throw new ArgumentException("email.SMTPHost", "specify email.SMTPHost");
var mail = new MailMessage();
if (string.IsNullOrEmpty(email.SenderDisplayName))
mail.From = new MailAddress(email.Sender);
else
mail.From = new MailAddress(email.Sender, email.SenderDisplayName);
if (!string.IsNullOrEmpty(email.ReplyTo))
{
if (string.IsNullOrEmpty(email.ReplyToDisplayName))
{
mail.ReplyToList.Add(new MailAddress(email.ReplyTo));
}
else
{
mail.ReplyToList.Add(new MailAddress(email.ReplyTo, email.ReplyToDisplayName));
}
}
foreach (string recipient in email.Recipients)
mail.To.Add(new MailAddress(recipient));
mail.Subject = email.Subject;
mail.Body = email.Body;
mail.IsBodyHtml = settings.IsBodyHtml;
if (email.CC != null && email.CC.Length > 0)
foreach (string cci in email.CC)
mail.CC.Add(cci);
if (email.BCC != null && email.BCC.Length > 0)
foreach (string bcci in email.BCC)
mail.Bcc.Add(bcci);
if (email.Attachments != null)
foreach (var ifn in email.Attachments)
mail.Attachments.Add(
new Attachment(
new MemoryStream(
ifn.Content),
ifn.FileName));
var smtpPort = settings.SMTPPort > 0 ? settings.SMTPPort : 25;
var smtpClient = new SmtpClient(settings.SMTPHost, smtpPort);
smtpClient.EnableSsl = settings.EnableSsl;
if (!string.IsNullOrEmpty(settings.SMTPUser))
{
smtpClient.UseDefaultCredentials = false;
var smtpPassword = settings.SMTPPassword == null ? string.Empty : settings.SMTPPassword;
NetworkCredential netCred;
if (string.IsNullOrEmpty(settings.SMTPDomain))
netCred = new NetworkCredential(settings.SMTPUser, smtpPassword);
else
netCred = new NetworkCredential(settings.SMTPUser, smtpPassword, settings.SMTPDomain);
smtpClient.Credentials = netCred;
}
else
smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
smtpClient.Timeout = 180 * 1000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpClient.Send(mail);
}