Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated

I'm trying to send email with my website's address from a C# application.
This worked fine for several months until recently. (maybe my provider changes some things or someone else changed settings)

Here's the code:

  private void sendEmail(Email invite) {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient(smtpServerName);
            mail.From = new MailAddress(emailUsername);

            mail.To.Add(invite.RecipientEmail);
            mail.Subject = invite.MessageSubject;
            mail.Body = invite.MessageBody;

            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);
//          SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
        }  

Here's the error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required.

Looking at other questions I tried what they suggested, to make SmtpServer.EnableSsl = true. This didn't work at all. It gave the following:

System.Net.Mail.SmtpException: Server does not support secure connections.

I'm guessing I should disable SSL and have it the way it was before.

Any suggestions how to make email sending work again?

EDIT
I've tried without SmtpServer.UseDefaultCredentials = false;
I've tried with it set to true: SmtpServer.UseDefaultCredentials =true;
I've tried commenting that line along with the following //SmtpServer.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);

like image 296
Adrian Avatar asked Dec 08 '12 02:12

Adrian


People also ask

What is System Net Mail Smtpexception?

Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation.

What does 5.7 0 authentication required mean?

The server response was: 5.7. 0 Authentication Required." You have received this error because Gmail, by default, does not allow you to send emails via SMTP using the mailbox username and password. They are your only restriction preventing attackers from hijacking your account.


2 Answers

That error message is typically caused by one of the following:

  • Incorrect connection settings, such as the wrong port specified for the secured or non-secured connection
  • Incorrect credentials. I would verify the username and password combination, to make sure the credentials are correct.
like image 71
Reed Copsey Avatar answered Oct 12 '22 15:10

Reed Copsey


I think you have to set DeliveryMethod = SmtpDeliveryMethod.Network

this one is currently working in my PC, just i checked,working nice,try this

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}
like image 28
Ravindra Bagale Avatar answered Oct 12 '22 17:10

Ravindra Bagale