smtp exception “failure sending mail”

2019-01-28 10:11发布

问题:

I created console application for sending Email

    public static void sendEmail(string email, string body)
    {
        if (String.IsNullOrEmpty(email))
            return;
        try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(email);
            mail.From = new MailAddress("test@gmail.com");
            mail.Subject = "sub";

            mail.Body = body;

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential("test@gmail.com", "admin@1234"); // ***use valid credentials***
            smtp.Port = 587;

            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch (Exception ex)
        {

        }
    }

I am using correct credential of my gmail account.

Is there any settings I need to do for GMail Account?

回答1:

It's likely that you're actually getting this error, it's just suppressed in some way by you being in a console app.

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

Change this piece of code and add one extra line. It's important that it goes before the credentials.

smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
// ** HERE! **
smtp.UseDefaultCredentials = false;
// ***********
smtp.Credentials = new System.Net.NetworkCredential("test@gmail.com", "admin@1234"); // ***use valid credentials***
smtp.Port = 587;

You'll also need to go here and enable less secure apps.

https://www.google.com/settings/security/lesssecureapps

Or if you have two step authentication on your account you'll need to set an app specific password, then use that password in your code instead of your main account password.

I've just tested and verified this works on my two step account. Hell, here's my entire method copied right out of LINQPad in case it helps (with removed details of course).

var fromAddress = new MailAddress("myaccount@gmail.com", "My Name");
var toAddress = new MailAddress("test.address@email.com", "Mr Test");
const string fromPassword = "tbhagpfpcxwhkczd";
const string subject = "test";
const string body = "HEY, LISTEN!";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout = 20000
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

Edit:

Using attachments:

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    Attachment attachment = new Attachment(filePath);
    message.Attachments.Add(attachment);

    smtp.Send(message);
}


回答2:

In my experienced using C# SMTP Client, there is a certain steps to follow in the code (see code snippet below)

smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential("account@gmail.com", "secretPassword");
smtpClient.Send(mail);