'Server does not support secure connections in C#

I'm getting the error "Server does not support secure connections" with my code below.

            SmtpClient smtp = new SmtpClient();
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("*****@gmail.com");
            mail.To.Add(recieverId);
            mail.Subject = "Invoice Copy and Delivery Confirmation for booksap.com Order " + orderno + ". Please Share Feedback.";
            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(Server.MapPath("OrderMail\\Invoice.pdf"));
            mail.Attachments.Add(attachment);
            MailBody = "We are pleased to inform that the following items in your order " + orderno + " have been placed. This completes your order. Thank you for shopping! ";
            StreamReader reader = new StreamReader(Server.MapPath("~/MailHTMLPage.htm"));
            string readFile = reader.ReadToEnd();
            string myString = "";
            myString = readFile;
            myString = myString.Replace("$$Name$$", ContactPersonName);
            myString = myString.Replace("$$MailBody$$", MailBody);
            mail.Body = myString.ToString();
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("*******@gmail.com", "*******");
            smtp.Send(mail);
            mail.Dispose();
            mail = null;

How can I fix this issue? If I set

 EnabledSsl = false

it will return the error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated.



Solution 1:[1]

That error message is typically caused by one of the following:

  • Incorrect connection settings, such as the wrong port specified for the secured or non-secured connection

  • Incorrect credentials. I would verify the username and password
    combination, to make sure the credentials are correct.

if it is ok,I think you have to set DeliveryMethod = SmtpDeliveryMethod.Network

simply try this..

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}

ALSO Change account access for less secure apps Go to the "Less secure apps" section in My Account.

Try Option 2:

Solution 2:[2]

@Abhishek Yes.The code that you have written will send the message to the server just check your mail box (***@gmail.com) but smtp client of google is preventing you from entering into thier domain. You have to make your gmail less secure that is you need to allow sign in attempt.enter image description here

Check Your mail box,you should have such response from gmail domain and you need to make your account less secure. enter image description here

I think its about captcha and bots preventing such logging in,people who know can throw some light in these things.Make your account less suspicious.Hope this helps

Solution 3:[3]

If you are working in any specific domain(company) then might be the company have personal proxy authentication so you will have to bypass proxy authentication for prevent error.

first you need to add

smtpServer.EnableSsl = false;

then add below code in your web.config

<system.net>
  <defaultProxy enabled="true" useDefaultCredentials="true">
    <proxy usesystemdefault="True" bypassonlocal="True" />
  </defaultProxy>
</system.net>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Hameed Syed
Solution 3 Peter Csala