0

i am getting

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

The server response was:

5.7.0 Authentication Required

...while sending Email in C#. How to solve it?

string to = "jane@contoso.com";
string from = "ben@contoso.com";
string subject = "Using the new SMTP client.";
string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient(server);
Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
client.Timeout = 100;
// Credentials are necessary if the server requires the client 
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;

try {
    client.Send(message);
    }  
catch (Exception ex) {
    }
Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
  • `CredentialCache.DefaultNetworkCredentials` will only work if you connect to a server using Windows authentication, eg Exchange. `DefaultNetworkCredentials` is the current Windows account. In any case the .NET [SmtpClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=net-7.0) class is obsolete with [a strong warning against its use](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=net-7.0#remarks) in the docs. – Panagiotis Kanavos Jan 09 '23 at 07:47

0 Answers0