我有一个网络应用程序,我想在我的应用程序中发送电子邮件。但我面对这个错误: SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.7.1需要身份验证。 这是我的代码示例:
public static bool SendMail(string subject, string body, params string[] toMails)
{
try
{
var mailMsg = new MailMessage();
mailMsg.BodyEncoding = Encoding.UTF8;
mailMsg.HeadersEncoding = Encoding.UTF8;
mailMsg.SubjectEncoding = Encoding.UTF8;
mailMsg.Priority = MailPriority.High;
mailMsg.Subject = subject;
mailMsg.Body = body;
mailMsg.IsBodyHtml = true;
mailMsg.From = new MailAddress("mail@yahoo.com","",Encoding.UTF8);
mailMsg.Sender = new MailAddress("mail@mail.com", "", Encoding.UTF8);
foreach (var mail in toMails)
{
mailMsg.To.Add(new MailAddress(mail));
}
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("mail@yahoo.com", "pass", "smtp.mail.yahoo.com");
smtp.Host = "smtp.mail.yahoo.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mailMsg);
return true;
}
catch
{
return false;
}
}
答案 0 :(得分:0)
我认为您的问题是使用的端口号。根据以下内容,TLS的端口465不是587.
http://support.sliqtools.co.uk/kb/a32/smtp-email-settings-to-send-from-yahoo-email-addresses.aspx
答案 1 :(得分:0)
删除此行,如果这是真的,则不使用您定义几行的凭据。
smtp.UseDefaultCredentials = true;