C# send email with office365 smtp

大城市里の小女人 提交于 2020-07-08 00:39:55

问题


I am using C# to send an email from my local computer, but I'm running into an error while executing the code below (for testing purposes).

C# code:

protected void SendEmail(string emailAddres)
{
    SmtpClient smtp = new SmtpClient();

    MailMessage mail = new MailMessage()
    {
        To = { new MailAddress(emailAddres) },
        From = new MailAddress("email@email.com", "fromEmail"),
        Subject = "Subject of a test email!",
        IsBodyHtml = true,
        Body = "Test body email.",
        BodyEncoding = System.Text.Encoding.UTF8,
        SubjectEncoding = System.Text.Encoding.UTF8
    };

   try
   {
       smtp.Send(mail);
       lblResultEmail.Text = "Message Sent Succesfully";
       lblResultEmail.ForeColor = System.Drawing.Color.Green;
   }
   catch (Exception ex)
   {
       lblResultEmail.Text = ex.ToString();
       lblResultEmail.ForeColor = System.Drawing.Color.Red;
   }
}

In the Web.config I added the below:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="smtp.office365.com" port="587" defaultCredentials="false" enableSsl="true"  
                 userName="email@email.com" password="ThisPwd" targetName="STARTTLS/smtp.office365.com"
        />
      </smtp>
    </mailSettings>
  </system.net>

Error message:

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [AM0PR05CA0077.eurprd05.prod.outlook.com] at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at...

Edit:

This could be a duplicate of SMTP 5.7.57 error when trying to send email via Office 365 But I couldn't find a solution there, so I'd like to reopen the discussion.


回答1:


The code in the question is OK. It was basically configuration on the Office365 account that needed to be done.

Check the following documentation by Microsoft for more information.

https://docs.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission#enable-smtp-auth-for-specific-mailboxes

If you've bought an Office365 email account with GoDaddy, ask them to enable SMTP AUTH. They'll enable it for you, but you still won't be able to use the Office365 smtp server on their hosting unless you're using a Virtual Private Server as GoDaddy blocks port 587 for shared hosting.



来源:https://stackoverflow.com/questions/62572583/c-sharp-send-email-with-office365-smtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!