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

5

20

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);

Fantail answered 8/12, 2012 at 2:18 Comment(5)
I recommend checking your provider's client connection settings, to make sure you've specified things correctly, such as the port, etc. Also check your user/pass, as these can cause that error message.Cammi
These credentials worked before, but I'm in the process of doing that. Doesn't hurt to be thorough. Thanks!Fantail
I can't recall for sure, but I think I have gotten that error when I was passing credentials and the server didn't require them. You might try commenting out the .Credentials line and seeing what happens.Sunfish
@ReedCopsey someone just got back to me. Credentials have been changed. If you want to make your comment into a reply I will accept it.Fantail
As @ReedCopsey said, checking credentials and settings is critical. I just used telnet to save me a lot of hassle. Here's an easy guide on how to do so.Swatow
C
8

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.
Cammi answered 10/12, 2012 at 19:44 Comment(0)
E
8

If you are sure that your Username and Password are correct, but you still get errors then it means that Gmail has blocked your application. Allow less secure apps in your Google Account settings

  • Visit this link to your Google Account settings for Less Secure App access.
  • Select your Google Account from which you are sending the mail.
  • Set Allow less secure apps to ON.
Eringo answered 27/2, 2017 at 6:59 Comment(1)
If your account has 2FA, you need to create an App Password Google App PasswordEdp
F
7

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);
}
Fulbright answered 8/12, 2012 at 2:44 Comment(1)
Default for DeliveryMethod is Network so this answer doesn't provide anything new.Fanchan
Q
1

You need to create an app password so that your application can bypass 2FA: Sign in with App Passwords Google App Password

Quixote answered 6/6, 2022 at 11:29 Comment(0)
M
0

If you are deploying the application on a server, try logging in your google account in your server. Bizarre thing is, smtp email sending app works fine in my desktop. Not in the server. This might be affected by how google's security mechanism works.

Madrepore answered 20/10, 2021 at 3:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.