电子邮件代码在本地主机上有效,但是当我在服务器上上传网站时显示错误 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);
}