Home > front end >  How do I send an email from my C# windows application?
How do I send an email from my C# windows application?

Time:01-12

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("[email protected]", "Bob Gatto");
    MailAddress to = new MailAddress("[email protected]", "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("<[email protected]>", "<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?

CodePudding user response:

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 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("<[email protected]>", "<App Password>");

You can find more info Here

CodePudding user response:

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.

  • Related