0

I am using send mail from office365 as below code


    MailMessage msg = new MailMessage();
    msg.To.Add(new MailAddress("*******", "*********"));
    msg.From = new MailAddress("******", "*******");
    msg.Subject = "This is a Test Mail";
    msg.Body = "This is a test message using Exchange OnLine";
    msg.IsBodyHtml = true;

    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("******", "********");
    client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
    client.Host = "d*******.mail.protection.outlook.com";//"smtp.office365.com";//
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = true;

    client.Send(msg);

When I used smtp.office365.com then I got below issue

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 [BM1PR01CA0089.INDPRD01.PROD.OUTLOOK.COM]

and if I use d**********.mail.protection.outlook.com then I got

Unable to read data from the transport connection: net_io_connectionclosed.

Makdous
  • 1,447
  • 1
  • 12
  • 24
ds test
  • 1
  • 1

1 Answers1

0

Try to re-order property setters by moving some of them up:

MailMessage msg = new MailMessage();
    msg.To.Add(new MailAddress("*******", "*********"));
    msg.From = new MailAddress("******", "*******");
    msg.Subject = "This is a Test Mail";
    msg.Body = "This is a test message using Exchange OnLine";
    msg.IsBodyHtml = true;

    SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential("******", "********");
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;    
    client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
    client.Host = "d*******.mail.protection.outlook.com";//"smtp.office365.com";//
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Send(msg);

Also, you may take a look at a similar thread - The server response was: 5.7.0 Must issue a STARTTLS command first. i16sm1806350pag.18 - gsmtp.

See similar forum threads.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45