电子邮件发送失败,我没有收到cofirmation电子邮件asp.net

时间:2016-12-17 13:35:45

标签: asp.net email

try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(UserName.Text);
            mail.From = new MailAddress("shammus672@gmail.com");
            mail.Subject = "Varificarion Code";
            string Body = "Hello";
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("shammus762@gmail.com", "shammus123");// Enter senders User name and password
            smtp.EnableSsl = true;
            smtp.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            string str = ex.ToString();
            return false;

        }

例外: " SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。在"

了解详情

我想向用户发送varification邮件。

1 个答案:

答案 0 :(得分:-1)

试试这个;

1 - 将这些命名空间添加到您的项目中;

使用System.Net.Mail;

使用System.Net;

2 - 使用此代码短语;

    MailMessage mail = new MailMessage();
    mail.IsBodyHtml = true;
    mail.To.Add(UserName.Text.ToString());

    mail.From = new MailAddress("shammus672@gmail.com", "Hello", System.Text.Encoding.UTF8);
    mail.Subject = "Verification Code";
    string Mesaj = "Hello";
    mail.Body = Mesaj;

    SmtpClient smtp = new SmtpClient();
    smtp.Credentials = new NetworkCredential("shammus762@gmail.com", "shammus123");
    smtp.Port = 587;
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    if (UserName.Text.ToString() != string.Empty)
    {
        smtp.Send(mail);
    }