简体   繁体   中英

How do I send an email from my C# windows application?

Im 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?

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:

在此处输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM