简体   繁体   中英

Server Connection using SMTP

I am trying to send a default passwords using smtp for users who want to retrieve their new passwords!

My Code :

public void SendUserPass()
{
    string sql = "Select Username, Password from Registration 
                  where Email='" + Email +   "'";
    DataTable table = new DataTable();
    table = db.RunQuery(sql);

    MailMessage message = new MailMessage("Esco@gmail.com", Email);
    message.Subject = "ESCO -Forgot Password";
    message.Body = "Username " + table.Rows[0][0].ToString() + "<br/> Password" +  
    table.Rows[0][1].ToString(); 

    message.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("Esco@gmail.com", "Mypassword");
    smtp.EnableSsl = true;
    smtp.Send(message);
}

I get this error :

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....

  Line 310: smtp.Send(message); 

Any help please ?

This worked for me

Login in to gmail ( in your case it is Esco@gmail.com)

visit

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

Enable access to less secure apps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM