尝试使用SmtpClient发送电子邮件时,我收到以下错误。
{" SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应是:5.7.57 SMTP;客户未通过身份验证,以便在MAIL FROM [DB6PR0501CA0021.eurprd05.prod.outlook.com]期间发送匿名邮件"}
我已经加倍检查所有密码是否正确,并通过office365从每个帐户成功发送电子邮件。
关于为什么会发生这种情况的任何想法?
public void SendMail(string smtpServer,
string userAccount,
string password,
string emailFrom,
string emailTo,
string emailCC,
string bcc,
string subject,
string body,
bool useSSL,
int port = -1,
List<Attachment> files = null)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress(emailFrom);
foreach (var address in emailTo.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
if (address != string.Empty)
mm.To.Add(address.RemoveWhitespace());
if (emailCC != null)
foreach (var address in emailCC.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
if (address != string.Empty)
mm.CC.Add(address.RemoveWhitespace());
mm.Body = body;//.Replace("\n",@"<br />");
// if (imageId > 0 || mm.Body.Contains(@"</") || mm.Body.Contains(@"/>"))
if (mm.Body.Contains(@"</") || mm.Body.Contains(@"/>"))
mm.IsBodyHtml = true;
else
mm.IsBodyHtml = false;
if (!string.IsNullOrEmpty(bcc))
mm.Bcc.Add(bcc.RemoveWhitespace());
// add attachments if present
if (files != null)
foreach (Attachment a in files)
{
mm.Attachments.Add(a);
}
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Timeout = int.MaxValue;
// SmtpMail.UseDefaultCredentials = false;
SmtpMail.Credentials = new System.Net.NetworkCredential(
userAccount,
password);
if (useSSL)
SmtpMail.EnableSsl = true;
SmtpMail.Host = smtpServer;
if (port != -1)
SmtpMail.Port = port;
Utility.WriteDebugLine(true, "SmtpMailSend");
SmtpMail.Send(mm);
}