1

I tried sending mail in my ASP.NET CORE project by C#. I ran code in emulation (IIS Express) but I get an error:

The SMTP server requires a secure connection or the client was not authenticated.

Please can you help me?

My code:

MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("formail@mail.com", "John Doe"));
msg.From = new MailAddress("frommail@mail.com", "Jane Doe");
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("frommail@mail.com", "Password");
client.Port = 587; 
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;

try
{
    client.Send(msg);
    return Content("Message sent successfully");
}
catch (Exception ex)
{
    return Content(ex.ToString());
}

Full error:

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 [AM3PR07CA0113.eurprd07.prod.outlook.com]

Thank you very much.

EDIT: I used code from this question. But I get error

System.Net.Sockets.SocketException (11001): No such host is known. My mail adress is me@gjb-spgs.cz.

My new code:

var sClient = new SmtpClient("gjbspgs-cz.mail.protection.outlook.com");
                var message = new MailMessage();

                sClient.Port = 25;
                sClient.EnableSsl = true;
                sClient.Credentials = new NetworkCredential("me@gjb-spgs.cz", "Password");
                sClient.UseDefaultCredentials = false;

                message.Body = "Test";
                message.From = new MailAddress("me@gjb-spgs.cz");
                message.Subject = "Test";
                message.To.Add(new MailAddress("someone@gmail.com"));

                sClient.Send(message);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
Martin
  • 33
  • 7
  • 2
    You need valid email addresses... already answered here https://stackoverflow.com/questions/30342884/5-7-57-smtp-client-was-not-authenticated-to-send-anonymous-mail-during-mail-fr – Aung Apr 11 '20 at 21:37
  • 1
    Does this answer your question? [5.7.57 SMTP - Client was not authenticated to send anonymous mail during MAIL FROM error](https://stackoverflow.com/questions/30342884/5-7-57-smtp-client-was-not-authenticated-to-send-anonymous-mail-during-mail-fr) – Jonatan Dragon Apr 11 '20 at 22:04
  • @JonatanDragon No, it not work. I get another error code. _System.Net.Sockets.SocketException (11001): No such host is known._ – Martin Apr 12 '20 at 21:04
  • Per the Official Documentation https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/pop3-and-imap4/pop3-and-imap4, your SMTP Server seems to be Wrong. Try using just Smtp.office365.com. – Sathish Guru V Apr 13 '20 at 04:30

0 Answers0