-2

Good day guys, I have a code that is used to send an email with attachment which I had referred from Finding the exact cause for the exception - System.Net.Sockets.SocketException

Here is the code:

namespace SendEmail
{
    class Email
    {
        public static void Main(string[] args)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            try
            {
                mail.From = new MailAddress("harish.1138@gmail.com");
                mail.To.Add("harish_1138@yahoo.com");
                mail.Subject = "Test Mail - 1";
                mail.Body = "mail with attachment";

                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment("C:\\EmailTest.xlsx");
                mail.Attachments.Add(attachment);

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("harish.1138@gmail.com", "SamplePWD");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("Mail Sent");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

When I execute this, I get an exception as this:

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

How do I fix the authentication required exception. Does this means something is blocking me from sending out the email?

Thanks a lot

Hari
  • 718
  • 3
  • 9
  • 30
  • 4
    The exception message appears to be in plain English, and provides very specific guidance as to what failed. I.e. you've either failed to use an encrypted connection, or failed to provide the necessary credentials for authentication. The code you posted appears to attempt to address both of these requirements, but without access to the actual email server and credentials, there's not much we can do here to help you. Lacking the ability to test first-hand, there are just too many different reasons you might get an error...you need to do some debugging yourself. – Peter Duniho Nov 27 '17 at 06:29
  • 1
    You should paste the exception as text, not as image. Also maybe duplicate of [this question](https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not) or [this question](https://stackoverflow.com/questions/18503333/the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated). there should be a lot reason that denies from using gmail smtp server. maybe you should use google's official API instead. – ymonad Nov 27 '17 at 06:30
  • Just added the exception message – Hari Nov 27 '17 at 06:35
  • According to this page, when using ssl you should use port 465 https://www.lifewire.com/what-are-the-gmail-smtp-settings-1170854 also https://support.google.com/a/answer/176600?hl=en – Erik Funkenbusch Nov 27 '17 at 06:41
  • How is this related to email attachments? I would suspect the same error to occur if you try to send the most plain `"Subject=Hello World"` email. Please don't make things complicated unless the problem in question only occurs with additional complexity. – grek40 Nov 27 '17 at 08:22

2 Answers2

1

I had make it to work... Referred here: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required? as suggested by @ymonad.

namespace SendEmail
{
    class Email
    {
        public static void Main(string[] args)
        {
            try
            {
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress("harish.1138@gmail.com");
                    mail.To.Add("harish_1138@yahoo.com");
                    mail.Subject = "Test Mail - 1";
                    mail.Body = "mail with attachment";

                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment("C:\\EmailTest.xlsx");
                    mail.Attachments.Add(attachment);

                    using (SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587))
                    {
                        SmtpServer.UseDefaultCredentials = false; //Need to overwrite this
                        SmtpServer.Credentials = new System.Net.NetworkCredential("harish.1138@gmail.com", "SamplePWD");
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                    }
                }

                MessageBox.Show("Mail Sent");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

And have to Turn on less secure apps as here: https://stackoverflow.com/a/38024407/5266708

And it works perfectly...

Hari
  • 718
  • 3
  • 9
  • 30
0

This exception is thrown, when you do not have access to the SMTP server.

Usually you log in to email with your password and username, when it comes to SMTP server requests, you send your IP as login credential.

If your IP is not authorized, you will not be allowed to use it and will be blocked.

You need to provide credentials, to override the IP authentication:

 SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    SmtpServer .Host = "mail.youroutgoingsmtpserver.com";
    SmtpServer Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");

If it does not allow you, then google is blocking your access.

Use this for reference:

Google mail api

Barr J
  • 10,636
  • 1
  • 28
  • 46