请问.NET 4在发送电子邮件的任何问题?我有一个.NET 3.5解决方案,它是工作然后迁移的解决方案,以.NET 4中,并没有作品;什么都没有改变!
注意: 我得到这个异常:
System.Net.Mail.SmtpException:SMTP服务器要求安全连接或客户端未通过身份验证。服务器响应为:5.5.1要求进行验证。了解更多 在System.Net.Mail.MailCommand.CheckResponse(SmtpStatus code状态code,字符串响应) 在System.Net.Mail.MailCommand.Send(SmtpConnection康涅狄格州,字节]命令,从字符串) 在System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人,收件人MailAddressCollection,字符串deliveryNotify,SmtpFailedRecipientException和放大器;除外) 在System.Net.Mail.SmtpClient.Send(MailMessage消息) 在...
这是我的code:
公共静态无效SendEmail(
此电子邮件的电子邮件)
{
如果(email.MailingSettings == NULL)抛出新ArgumentNullException(email.MailingSettings,指定email.MailingSettings);
VAR设置= email.MailingSettings;
如果(string.IsNullOrEmpty(email.Sender))抛出新的ArgumentException(email.Sender,指定发件人);
如果(email.Recipients == NULL)抛出新ArgumentNullException(email.Recipients,至少指定收件人);
如果(email.Recipients.Length == 0)抛出新ArgumentNullException(email.Recipients,至少指定收件人);
如果(string.IsNullOrEmpty(settings.SMTPHost))抛出新的ArgumentException(email.SMTPHost,指定email.SMTPHost);
VAR邮件=新MailMessage();
如果(string.IsNullOrEmpty(email.SenderDisplayName))
mail.From =新MailAddress(email.Sender);
其他
mail.From =新MailAddress(email.Sender,email.SenderDisplayName);
如果(!string.IsNullOrEmpty(email.ReplyTo))
{
如果(string.IsNullOrEmpty(email.ReplyToDisplayName))
{
mail.ReplyToList.Add(新MailAddress(email.ReplyTo));
}
其他
{
mail.ReplyToList.Add(新MailAddress(email.ReplyTo,email.ReplyToDisplayName));
}
}
的foreach(在email.Recipients串接收方)
mail.To.Add(新MailAddress(收件人));
mail.Subject = email.Subject;
mail.Body = email.Body;
mail.IsBodyHtml = settings.IsBodyHtml;
如果(email.CC =空&安培;!&安培; email.CC.Length大于0)
的foreach(在email.CC串CCI)
mail.CC.Add(CCI);
如果(email.BCC =空&安培;!&安培; email.BCC.Length大于0)
的foreach(在email.BCC串BCCI)
mail.Bcc.Add(BCCI);
如果(email.Attachments!= NULL)
的foreach(在email.Attachments VAR IFN)
mail.Attachments.Add(
新的附件(
新的MemoryStream(
ifn.Content)
ifn.FileName));
VAR SMTPPORT = settings.SMTPPort> 0? settings.SMTPPort:25;
VAR smtpClient =新SmtpClient(settings.SMTPHost,SMTPPORT);
smtpClient.EnableSsl = settings.EnableSsl;
如果(!string.IsNullOrEmpty(settings.SMTPUser))
{
smtpClient.UseDefaultCredentials = FALSE;
VAR smtpPassword = settings.SMTPPassword == NULL?的String.Empty:settings.SMTPPassword;
的NetworkCredential netCred;
如果(string.IsNullOrEmpty(settings.SMTPDomain))
netCred =新的NetworkCredential(settings.SMTPUser,smtpPassword);
其他
netCred =新的NetworkCredential(settings.SMTPUser,smtpPassword,settings.SMTPDomain);
smtpClient.Credentials = netCred;
}
其他
smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
smtpClient.Timeout = 180 * 1000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
ServicePointManager.ServerCertificateValidationCallback =
委托(对象S,X509证书证书,X509Chain链,SslPolicyErrors sslPolicyErrors)
{返回true; };
smtpClient.Send(邮件);
}
解决方案
这code是正确的和作品。问题是Gmail上侧和解决的(未检测到为何以及如何)。
Does .NET 4 has any problems in sending emails? I had a .NET 3.5 solution and it was working then migrated the solution to .NET 4 and It does not works; nothing changed!
Notes: I get this exception:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at ...
And this is my code:
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);
}
解决方案
This code is correct and works. The problem was on GMail side and solved (not detected why and how).