Reputation: 1162
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("[email protected]");
message.To.Add("[email protected]");
message.Subject = "Test Email";
message.Body = "Email Body";
SmtpServer.Port = 587;
SmtpServer.Credentials = new
System.Net.NetworkCredential("[email protected]", "xxxxxxxx");
//SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 60000; // 60 seconds
SmtpServer.Send(message);
Upvotes: 0
Views: 13200
Reputation: 1162
below code worked for me
System.Net.Mail.AlternateView htmlView = null;
string from = "[email protected]";
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("[email protected]", "xxxxxxxx"); // username and password
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.Send(mail);
}
Upvotes: 1
Reputation: 11
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'
Upvotes: 1