使用Gmail从asp.net发送电子邮件

问题描述:

我试图发送使用asp.net gmail邮箱

I am trying to send mail using gmail in asp.net

我的code:

using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
        {
            mm.Subject = txtSubject.Text;
            mm.Body = txtBody.Text;
            if (fuAttachment.HasFile)
            {
                string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
                mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
            }
            mm.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
        }

但我得到的错误:

But i am getting 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

哪里是我的错误?

where is my error?

您可以尝试以下建议:


  1. 尝试设置UseDefaultCredentials到假 smtp.UseDefaultCredentials = FALSE;

  2. 尝试登录到您的Gmail帐户,它可能阻止访问。

  3. 尝试使您的Gmail帐户的访问,看到这个链接了解更多信息,http://email.about.com/od/gmailtips/qt/How-To-Unlock-Gmail-For-A-New-Email-Program-Or-Service.htm

  1. Try setting "UseDefaultCredentials" to "False" smtp.UseDefaultCredentials = false;
  2. Try logging to your Gmail account, it may be blocking the access.
  3. Try enabling access to your Gmail account, see this link for more information http://email.about.com/od/gmailtips/qt/How-To-Unlock-Gmail-For-A-New-Email-Program-Or-Service.htm