Sending Email via GMail and .NET 4

南笙酒味 提交于 2019-12-04 18:30:03

If everything is as per posted-code and comments then I can say that you've turned on 2-step verification of your google account and in that case you need to generate Application Specific-password.

This code is correct and works. The problem was on GMail side and solved (not detected why and how).

ori

Try to use this :

try
{
    string smtpAddress = "smtp.gmail.com";
    int portNumber = 587;
    bool enableSSL = true;
    string emailFrom = "senderemail@gmail.com";
    string password = "password";
    string emailTo = "receiver@gmail.com";
    string subject = "Halo!";
    string body = "Halo, Mr. SMTP";
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;

    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
        smtp.Credentials = new NetworkCredential(emailFrom, password);
        smtp.EnableSsl = enableSSL;
        smtp.Send(mail);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

*Note before that you need to activated this : https://myaccount.google.com/lesssecureapps

and then build and run the code.. it's amazing.. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!