0

I try to send an email (with gmail) in C#. I check the solutions I can find on google but it never works for me, I always have the error :

SMTP server requires a secure connection or the client was not authenticated. The response from the server Was: 5.5.1 Authentication Required.

This is an exampe of code :

string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;

string emailFrom = "*****@gmail.com";
string password = "*****";
string emailTo = "******@gmail.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;

    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
        smtp.Credentials = new NetworkCredential(emailFrom, password);
        smtp.EnableSsl = enableSSL;
        smtp.Send(mail);
    }
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
baudic_j
  • 45
  • 1
  • 10
  • Does the email account you are trying to send email through have two-factor authentication enabled? – Tieson T. Jul 13 '16 at 06:47
  • 3
    Possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Tieson T. Jul 13 '16 at 06:49

2 Answers2

0

The MSDN says that : Some SMTP servers require that the client be authenticated before the server sends e-mail on its behalf. Set this property to true when this SmtpClient object should, if requested by the server, authenticate using the default credentials of the currently logged on user. For client applications, this is the desired behavior in most scenarios.

So you need to set smtp.UseDefaultCredentials = false; before assigning Credentials, So your code will looks like the following:

using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential(emailFrom, password);
    smtp.EnableSsl = enableSSL;
    smtp.Send(mail);
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

I find the solution which works for me. The solution proposed by un-lucky was good but I need to configure gmail account.

So the code is :

        string smtpAddress = "smtp.gmail.com";
        int portNumber = 587;
        bool enableSSL = true;

        string emailFrom = "****@gmail.com";
        string password = "*****";
        string emailTo = "****@gmail.com";
        string subject = "Hello";
        string body = "Hello, I'm just writing this to say Hi!";

        using (MailMessage mail = new MailMessage())
        {
            mail.From = new MailAddress(emailFrom);
            mail.To.Add(emailTo);
            mail.Subject = subject;
            mail.Body = body;

            using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
            {
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(emailFrom, password);
                smtp.EnableSsl = enableSSL;
                smtp.Send(mail);
            }
        }

Moreover in your gmail account, you have to make this changes.

gmail account changes

Community
  • 1
  • 1
baudic_j
  • 45
  • 1
  • 10