我想通过我的ASP.Net网站发送电子邮件

时间:2018-07-05 18:30:35

标签: asp.net asp.net-mvc email asp.net-mvc-4

电子邮件代码在本地主机上有效,但是当我在服务器上上传网站时显示错误 SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证。了解更多信息

//Formatted
protected void btnSend_Click(object sender, EventArgs e)
{

            var fromAddress = "djdanny1255@gmail.com";
              string email = "djdanny1255@gmail.com";
            var toAddress = email;
            const string fromPassword = "********";
            string subject = "Email=" + txtEmail.Text + "     Phone=" + txtMobile.Text;
            string body = txtMessage.InnerText;



            try
            {
                using (MailMessage mm = new MailMessage(fromAddress, email))
                {

                    mm.Subject = subject;
                    mm.Body = body;

                    mm.IsBodyHtml = false;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential NetworkCred = new NetworkCredential(fromAddress, fromPassword);
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    smtp.Port = 587;
                    smtp.Send(mm);

                }
            }
            catch (Exception ex)
            {

                Response.Write("Error" + ex.Message);
            }

1 个答案:

答案 0 :(得分:0)

在第一个cas中,我建议您更改此代码中的代码:

@escaping

登录电子邮件后的第二秒CLICK HERE

这将看到此页面 enter image description here

我希望这对您有所帮助^^