Sending email using Smtp.mail.microsoftonline.com

后端 未结 5 1889
梦如初夏
梦如初夏 2021-01-04 12:28

The context:

We’re a small company that does not have an Exchange Server (or anyone dedicated to it) yet we still need to have/send emails.

相关标签:
5条回答
  • 2021-01-04 13:07

    looking in Reflector on UseDefaultCredentials property, you can see that it also changes the trasnport.Credentials value, so when you called this property with a false value, it changed the transport credentials to null. the problem is that you called this property after setting the credentials in the line before that, it nullified the credentials.

    so bottom line, you shouldn't set the credentials and call this property afterwise.

    0 讨论(0)
  • 2021-01-04 13:10

    5.7.1 is not an authentication issue, but a relay issue. In order to prevent anyone from using your server (or account, as the case may be) the smtp server is configured to only allow mail to users outside your domain if it is comming from an authoritive address. Verify that the address you have listed here

    mail.From = new MailAddress("xxx@domain.com");
    

    is the same as the one you are authenticating as. Also, make sure that the domain of the address listed is in the authoritive domains list.

    0 讨论(0)
  • 2021-01-04 13:16

    What worked for me was what the-dude suggested Send SMTP email using System.Net.Mail via Exchange Online (Office 365) , on changing the email "from" address to be the same as the login for the stmp address

    AND

    doing what Vince suggested at the end Sending email using Smtp.mail.microsoftonline.com for commenting out "smtpClient.UseDefaultCredentials = false;"

    0 讨论(0)
  • 2021-01-04 13:18

    you can try this sample

          private void Button1_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                MailMessage myMessage = new MailMessage();
                SmtpClient myClient = new SmtpClient("yourserver");
                myClient.Port = "587";
                myClient.Host = "your server";
                myClient.UseDefaultCredentials = false;
                myClient.Credentials = new System.Net.NetworkCredential("username", "password");
    
    
                myMessage.From = new MailAddress("sender");
                myMessage.To.Add("recipient");
                myMessage.Subject = "Subject email";
                myMessage.Body = "body email";
                myClient.EnableSsl = true;
                myClient.Send(myMessage);
            }
    
            catch (Exepiton ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    

    Bye

    0 讨论(0)
  • 2021-01-04 13:27

    Be sure to double check that your username is correct. It is not necessarily the same as your from email address. For example the from address may be "no-reply@yourdomain.com" but the username could be "mail-svc@yourdomain.onmicrosoft.com" depending no your setup and integration with Azure Active Directory etc.

    0 讨论(0)
提交回复
热议问题