12

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

3
  • 1
    Can you post the entire exception message. Also, in your gmail security settings, allow less-secure applications. Aug 26, 2015 at 16:09
  • If I recall correctly, GMail supports only SMTPS access. Aug 26, 2015 at 16:16
  • I enabled less secure apps. Its working thank you so much.
    – user5264652
    Aug 26, 2015 at 18:39

2 Answers 2

16

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("[email protected]", "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("[email protected]", "My Name");
var toAddress = new MailAddress("[email protected]", "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
  • Here how can use Attachments?
    – user5264652
    Aug 26, 2015 at 18:40
  • 1
    Added in an example of attachments :-)
    – Equalsk
    Aug 26, 2015 at 20:50
0

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("[email protected]", "secretPassword");
smtpClient.Send(mail);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.