0

I.m working on a Windows app where the user has to enter a password. I also have a "Forgot Password" link. on the window. When that's clicked, I have the user enter their email address and click a Submit button. Every time they enter an email address and click the button, I get the error message:

SmtpException has occured: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.

The code I'm using is:

try
{
    Cursor = Cursors.WaitCursor;

    MailAddress from = new MailAddress("bgatt64@gmail.com", "Bob Gatto");
    MailAddress to = new MailAddress("bgatto64@yahoo.com", "Bob Gatto");

    MailMessage eMsg = new MailMessage(from, to);
    eMsg.Subject = "Your Password Renewal Request";
    eMsg.IsBodyHtml = true;
    eMsg.Body = "This is the body.";

    SmtpClient eClient = new SmtpClient("smtp.gmail.com", 587);
    eClient.EnableSsl = true;
    eClient.UseDefaultCredentials = true;gmail

    // The following email and password used is that of my own gmail email
    // that I use for my own personal email.

    eClient.Credentials = new System.Net.NetworkCredential("<MyOwnEmail@gmail.com>", "<MyPassword>");
    eClient.Send(eMsg);
}
catch (SmtpException ex)
{
    throw new ApplicationException("SmtpException has occurred: " + ex.Message);
}
catch (Exception ex)
{
    throw ex;
}

What else needs to be done?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    As an aside... `throw ex;` is an anti-pattern. It strips useful information from the exception before re-throwing it. To re-throw the original exception just use `throw;` Or remove that `catch` block entirely since it isn't doing anything. – David Jan 09 '23 at 20:25
  • 1
    Try port 465. From docs: `In the Port field, enter one of the following numbers: For SSL, enter 465. For TLS, enter 587.` – hijinxbassist Jan 09 '23 at 20:26
  • Does this answer your question? [How to send an e-mail with C# through Gmail](https://stackoverflow.com/questions/29465096/how-to-send-an-e-mail-with-c-sharp-through-gmail) – quaabaam Jan 09 '23 at 20:28

2 Answers2

0

You cannot use your plain google account password to authenticate to Gmail SMTP server anymore as Google requires two-step authentication now. You'll need to use the App Password instead:

enter image description here

You'll get the string with the 4 groups of characters. Now you need to use it in your code:

eClient.Credentials = new System.Net.NetworkCredential("<MyOwnEmail@gmail.com>", "<App Password>");

You can find more info Here

  • By the way, you don't need to use your Gmail account for email sending. You can try to use AWS Simple Email Service. It has 62000 free outgoing emails per month - https://aws.amazon.com/ses/pricing/?nc1=h_ls Or maybe you can look at the SendGrid service - https://sendgrid.com/pricing/ – Igor Martynov Jan 09 '23 at 21:41
0

After the removal of less secure apps you can no longer use your actual google password to connect to the smpt server you need to create an apps password.

How to create a Apps Password for connecting to Google's SMTP server.

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

namespace GmailSmtpConsoleApp
{
    class Program
    {
        private const string To = "[redacted]@gmail.com";
        private const string From = "[redacted]@gmail.com";
        
        private const string GoogleAppPassword = "";
        
        private const string Subject = "Test email";
        private const string Body = "<h1>Hello</h1>";
        
        
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            var smtpClient = new SmtpClient("smtp.gmail.com")
            {
                Port = 587,
                Credentials = new NetworkCredential(From , GoogleAppPassword),
                EnableSsl = true,
            };
            var mailMessage = new MailMessage
            {
                From = new MailAddress(From),
                Subject = Subject,
                Body = Body,
                IsBodyHtml = true,
            };
            mailMessage.To.Add(To);

            smtpClient.Send(mailMessage);
        }
    }
}

Note: if you are trying to connect users then you could also use Xoauth2 but using an apps password is far easer solution.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449