The server response was: 5.7.0 Must issue a STARTTLS command first. i16sm1806350pag.18 - gsmtp

217,781

Solution 1

Step (1): smtp.EnableSsl = true;

if not enough:

Step (2): "Access for less secure apps" must be enabled for the Gmail account used by the NetworkCredential using google's settings page:

Solution 2

Gmail requires you to use a secure connection. This can be set in your web.config like this:

<network host="smtp.gmail.com" enableSsl="true" ... />

OR

The SSL should be enable on the webserver as well. Refer following link

Enabling SSL on IIS 7.0

Solution 3

This issue haunted me overnight as well. Here's how to fix it:

  • Set host to: smtp.gmail.com
  • Set port to: 587

This is the TLS Port. I had been using all of the other SMTP ports with no success. If you set enableSsl = true like this:

Dim SMTP As New SmtpClient(HOST)
SMTP.EnableSsl = True

Trim the username and password fields (good way to prevent errors if user inputs the email and password upon registering like mine does) like this:

SMTP.Credentials = New System.Net.NetworkCredential(EmailFrom.Trim(), EmailFromPassword.Trim())

Using the TLS Port will treat your SMTP as SMTPS allowing you to authenticate. I immediately got a warning from Google saying that my email was blocking an app that has security risks or is outdated. I proceeded to "Turn on less secure apps". Then I updated the information about my phone number and google sent me a verification code via text. I entered it and voila!

I ran the application again and it was successful. I know this thread is old, but I scoured the net reading all the exceptions it was throwing and adding MsgBoxes after every line to see what went wrong. Here's my working code modified for readability as all of my variables are coming from MySQL Database:

Try
    Dim MySubject As String = "Email Subject Line"
    Dim MyMessageBody As String = "This is the email body."
    Dim RecipientEmail As String = "[email protected]"
    Dim SenderEmail As String = "[email protected]"
    Dim SenderDisplayName As String = "FirstName LastName"
    Dim SenderEmailPassword As String = "SenderPassword4Gmail"

    Dim HOST = "smtp.gmail.com"
    Dim PORT = "587" 'TLS Port
    
    Dim mail As New MailMessage
    mail.Subject = MySubject
    mail.Body = MyMessageBody
    mail.To.Add(RecipientEmail) 
    mail.From = New MailAddress(SenderEmail, SenderDisplayName)

    Dim SMTP As New SmtpClient(HOST)
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential(SenderEmail.Trim(), SenderEmailPassword.Trim())
    SMTP.DeliveryMethod = SmtpDeliveryMethod.Network 
    SMTP.Port = PORT
    SMTP.Send(mail)
    MsgBox("Sent Message To : " & RecipientEmail, MsgBoxStyle.Information, "Sent!")
Catch ex As Exception
    MsgBox(ex.ToString)
End Try

I hope this code helps the OP, but also anyone like me arriving to the party late. Enjoy.

Solution 4

"https://www.google.com/settings/security/lesssecureapps" use this link after log in your gmail account and click turn on.Then run your application,it will work surely.

Solution 5

SEND Button logic:

string fromaddr = "[email protected]";
string toaddr = TextBox1.Text;//TO ADDRESS HERE
string password = "YOUR PASSWROD";

MailMessage msg = new MailMessage();
msg.Subject = "Username &password";
msg.From = new MailAddress(fromaddr);
msg.Body = "Message BODY";
msg.To.Add(new MailAddress(TextBox1.Text));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
NetworkCredential nc = new NetworkCredential(fromaddr,password);
smtp.Credentials = nc;
smtp.Send(msg);

This code work 100%. If you have antivirus in your system or firewall that restrict sending mails from your system so disable your antivirus and firewall. After this run this code... In this above code TextBox1.Text control is used for TOaddress.

Share:
217,781
Ankur Gupta
Author by

Ankur Gupta

Working as a software Developer in .Net Technology, and i like coding &amp; solve the code related problem. i am passionate about coding and learn new technology.

Updated on July 08, 2022

Comments

  • Ankur Gupta
    Ankur Gupta almost 2 years

    I am trying to send mail using gmail, and I am getting an exception that is The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. i16sm1806350pag.18 - gsmtp

    code I have written for sending mail is:

    MailMessage mail = new MailMessage(); 
    mail.To.Add(txtEmail.Text.Trim()); 
    mail.To.Add("[email protected]");
    mail.From = new MailAddress("[email protected]");
    mail.Subject = "Confirmation of Registration on Job Junction.";
    string Body = "Hi, this mail is to test sending mail using Gmail in ASP.NET";
    mail.Body = Body;
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    // smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
    smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
    // smtp.Port = 587;
    //Or your Smtp Email ID and Password
    smtp.UseDefaultCredentials = false;
    // smtp.EnableSsl = true;
    smtp.Send(mail);
    

    Please tell me solutions, I am not getting any solutions for this exception.