Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# sending email via Gmail account

Tags:

c#

smtp

gmail

I am extending my Web App with simple CMD "Service", which should send verification email on newly registered user. My problem is by authenticating the Gmail account, where following exception is thrown:

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

I have tried authenticate on my own IMAP server, but it did not worked out too. Later I tried to use XAMP Mercury email server, which is not the best solution, due to depending entirely on local configuration, so I abandoned that idea. In future, i want to create a new google account for the app only, so no maintenance is required.

  String body = "<head>" +
            "Here comes some logo" +
          "</head>" +
          "<body>" +
            "<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
            "<a>Dear User, </a>" + Environment.NewLine +
            "<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
            "<a>This is the last step towards using our app.</a>" + Environment.NewLine +
            "<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
            "<a>[Callback url]</a>" +
          "</body>";
  try
  {
     SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
     smtpClient.UseDefaultCredentials = false;
     smtpClient.Credentials = new NetworkCredential()
     {
        UserName = "[email protected]",
        Password = "mypassword"
     };
     smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
     smtpClient.EnableSsl = true;
     smtpClient.Send("[email protected]", "[email protected]", "Account verification", body);
  }
  catch (Exception ex)
  {
  }

I just want to be able to send an email via Gmail server, without any exceptions. Do I need any NuGet packages for that, use different approach?

like image 507
Hugo Vrana Avatar asked Oct 17 '25 15:10

Hugo Vrana


1 Answers

If your Gmail account has 2-Step Verification enabled you will have to create an App-Specific Password to authenticate with instead.

Note also that SmtpClient is IDisposable - you should be putting it in a using (var smtpClient = new SmtpClient("smtp.gmail.com", 587)) { ... } block so that the SMTP connection RSETs, QUITs and closes correctly.

== edit ==

Also, it appears you have the from and recipients parameters switched around on smtpClient.Send.

string body = "<head>" +
            "Here comes some logo" +
        "</head>" +
        "<body>" +
            "<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
            "<a>Dear User, </a>" + Environment.NewLine +
            "<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
            "<a>This is the last step towards using our app.</a>" + Environment.NewLine +
            "<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
            "<a>[Callback url]</a>" +
        "</body>";
try
{
    using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
    {
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = new NetworkCredential()
        {
            UserName = Config.Username,
            Password = Config.Password,
        };
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = true;

        //Oops: from/recipients switched around here...
        //smtpClient.Send("[email protected]", "[email protected]", "Account verification", body);
        smtpClient.Send("[email protected]", "[email protected]", "Account verification", body);
    }
}
catch (Exception e)
{
    Console.Error.WriteLine("{0}: {1}", e.ToString(), e.Message);
}
like image 160
AlwaysLearning Avatar answered Oct 20 '25 03:10

AlwaysLearning



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!