SMTP邮件与ASP.Net一起发送

时间:2018-05-02 12:57:48

标签: c# asp.net smtp office365 port

我试图找出为什么我无法使用 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]

2 个答案:

答案 0 :(得分:1)

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

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;
            }
        }

答案 1 :(得分:0)

它没有附件吗?

尝试在凭据中添加主机:

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