SMTP 邮件随 ASP.Net 一起发送



我试图弄清楚为什么我无法使用office365帐户发送带有附件的简单邮件,如果我的代码有错误或帐户配置丢失,我不知道,请帮助代码:

代码是:

protected void sentmail(string sb)
{ 
using (var msg = new MailMessage())
{
msg.To.Add(new MailAddress("myaccount@company.cl));
msg.From = new MailAddress("botmail@company.cl");
msg.ReplyToList.Add("botmail@company.cl");
msg.Subject = "Test Mail";
msg.Body = sb.ToString();
msg.IsBodyHtml = true;
var client = new SmtpClient
{
Host = "smtp.office365.com", //it worked on testmail
Credentials = new System.Net.NetworkCredential("botmail@company.cl, "password"),
Port = 25,
EnableSsl = true
};
client.Send(msg);
}
}

发送时的错误是:

5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [SC1PR80CA0082.lamprd80.prod.outlook.com]

这是我现有的生产代码,可以为您发送

using (var msg = new MailMessage())
{
msg.To.Add(new MailAddress(userName));
msg.From = new MailAddress(userName);
msg.ReplyToList.Add(model.EmailAddress);
msg.Subject = "Message from the Web";
msg.Body = sb.ToString();
msg.IsBodyHtml = true;
var client = new SmtpClient
{
Host = "xxxmydomainxxx-co-uk.mail.protection.outlook.com",
Credentials = new System.Net.NetworkCredential(userName, password),
Port = 25,
EnableSsl = true
};
// You can use Port 25 if 587 is blocked 
try
{
client.Send(msg);
return true;
}
catch (SmtpException smtpEx)
{
return smtpEx;
}
catch (Exception ex)
{
return ex;
}
}

它没有附件即可工作?

尝试在凭据中添加主机:

credentials = new System.Net.NetworkCredential(MAIL_CREDENTIALS_EMAIL, PASS_CREDENTIALS_EMAIL, HOST_EMAIL);

最新更新