0

I am trying to send email through my asp.net Application and it is throwing the error "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 [SG2PR0601CA0003.apcprd06.prod.outlook.com]"

       MailMessage message = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
        message.From = new MailAddress("xxx@outlook.com");
        message.To.Add("xxx@gmail.com");
        message.Subject = "Test Email";
        message.Body = "Email Body";
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new 
         System.Net.NetworkCredential("xxx@outlook.com", "xxxxxxxx");
        //SmtpServer.EnableSsl = true;
        SmtpServer.Timeout = 60000; // 60 seconds
        SmtpServer.Send(message);
Mohan Singh
  • 1,142
  • 3
  • 15
  • 30
  • .Net version 4.5 and windows 10. – Mohan Singh Dec 06 '17 at 04:56
  • Possible duplicate of [Cannot send email through Hotmail / live.com / outlook.com](https://stackoverflow.com/questions/25224768/cannot-send-email-through-hotmail-live-com-outlook-com) – wp78de Dec 06 '17 at 05:13

2 Answers2

1

1) Check the account credentials by logging in on the owa (www.outlook.com)
2) please check the link below as this will be the solution to your question
https://www.codeproject.com/Articles/700211/Csharp-SMTP-Configuration-for-Outlook-Com-SMTP-Hos

The 2 differences I already see are below
- SmtpServer.EnableSsl = true;
- new SmtpClient("smtp-mail.outlook.com")

In the article is also a cool section 'Use the App Password Instead of Your Outlook.Com Account Password'

  • it throws the error " Server does not support secure connections." when i change SmtpServer.EnableSsl = true; – Mohan Singh Dec 05 '17 at 06:32
  • tried all these ,my login credentials are fine i am able to login to www.outlook.com, but still getting the same error. – Mohan Singh Dec 05 '17 at 06:42
  • Have you tried using the code in the [article](https://www.codeproject.com/Articles/700211/Csharp-SMTP-Configuration-for-Outlook-Com-SMTP-Hos) that I mentioned? Maybe create a new test project and use that code with your credentials – Bart Van den Broeck Dec 05 '17 at 13:27
1

below code worked for me

    System.Net.Mail.AlternateView htmlView = null;
        string from = "xxx@outlook.com";
        using (MailMessage mail = new MailMessage(from, txtEmail.Text.Trim()))
        {
            mail.Subject = "Json File";
            htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString("<html><body><div style='border-style:solid;border-width:5px;border-radius: 10px; padding-left: 10px;margin: 20px; font-size: 18px;'> <p style='font-family: Vladimir Script;font-weight: bold; color: #f7d722;font-size: 48px;'>Kindly find the Attachment.</p><hr><div width=40%;> <p  style='font-size: 20px;'>Thanks</div></body></html>", null, "text/html");
            mail.AlternateViews.Add(htmlView);
            mail.IsBodyHtml = true;
            System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
            contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
            contentType.Name = "New-Assign04.json";
            mail.Attachments.Add(new Attachment(Server.MapPath("~/App_Data/New-Assign04.json"), contentType));
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp-mail.outlook.com";
            smtp.EnableSsl = true;
            NetworkCredential networkCredential = new NetworkCredential("xxx@outlook.com", "xxxxxxxx");   // username and password
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = networkCredential;
            smtp.Port = 587;
            smtp.Send(mail);
        }
Mohan Singh
  • 1,142
  • 3
  • 15
  • 30