我已将我的网站部署到azure云,我正在尝试向客户端发送电子邮件。当我在本地主机上测试它时,它可以正常工作,但在移动到云时,它会显示有时出现的几个错误:
- >连接超时,我已将超时声明为150000
- > SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。 (我已将不太安全的应用程序允许邮寄帐户)
这里我附上了我的代码。请查看它并建议解决方法。 提前谢谢大家。
protected void Button2_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
MailAddress from = new MailAddress("bvpcsccloud@gmail.com");
MailAddress to = new MailAddress(Label7.Text);
string subjectText = "E-appointment request";
string bodyText = @"Hi, This mail is for the request of e-appointment from our patient:"+TextBox4.Text.ToString()+"<br />"+ "The patient has age:";
bodyText += TextBox5.Text.ToString()+"<br /> Disease symbols are"+TextBox2.Text.ToString()+"<br /> Disease name:"+ TextBox3.Text.ToString();
bodyText += "<br />Patient address is" + TextBox1.Text.ToString();
bodyText += "<br /> preffered date choosen by patient is" + DropDownList1.SelectedItem.Text + "-" + DropDownList2.SelectedItem.ToString() + "-" + DropDownList3.SelectedItem.Text;
msg.To.Add(to);
msg.From = from;
msg.Subject = subjectText;
msg.Body = bodyText;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("bvpcsccloud@gmail.com", "password");
//smtp.UseDefaultCredentials = false;
try
{
smtp.EnableSsl = true;
smtp.Timeout = 150000;
smtp.Send(msg);
smtp.Dispose();
Response.Redirect("~/appointment_booked.aspx");
}
catch (Exception ex)
{
throw ex;
}
}