做NET4在发送电子邮件方面有什么问题吗?我吃了一顿饭。NET 3.5解决方案,然后将该解决方案迁移到。NET 4,但它不起作用;什么都没变!
注意:我得到这个例外:
系统网邮政SmtpException:SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5。1需要身份验证。在系统中了解更多信息。网邮政MailCommand。在系统中检查响应(SmtpStatusCode statusCode,字符串响应)
。网邮政MailCommand。在系统上发送(SmtpConnection conn,Byte[]命令,String from)
。网邮政短程运输。SendMail(MailAddress发件人、MailAddressCollection收件人、字符串deliveryNotify、SmtpFailedRecipientException
这是我的代码:
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);
}
如果一切都是按照张贴的代码和评论,那么我可以说,你已经打开了谷歌帐户的两步验证,在这种情况下,你需要生成特定于应用程序的密码。
此代码正确且有效。这个问题在GMail方面得到了解决(没有检测到原因和方式)。
尝试使用这个:
try
{
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "senderemail@gmail.com";
string password = "password";
string emailTo = "receiver@gmail.com";
string subject = "Halo!";
string body = "Halo, Mr. SMTP";
MailMessage mail = new MailMessage();
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
*请注意,在此之前,您需要激活:https://myaccount.google.com/lesssecureapps
然后构建并运行代码。。太神奇了……)