我目前正在尝试通过 System.Net.Mail.MailMessage 发送电子邮件,但是当我运行我的代码时,我生成了这个异常,发送邮件失败。内部异常是:无法连接到远程服务器。我相信我正在连接到远程服务器,但很好奇为什么会出现此异常这是我的代码的外观:
try
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.Subject = "Testing Email";
msg.From = new System.Net.Mail.MailAddress("test@gmail.com");
msg.To.Add(new System.Net.Mail.MailAddress("isavepluscom@gmail.com"));
msg.Body = "testing the email";
System.Net.Mail.SmtpClient smpt = new System.Net.Mail.SmtpClient();
smpt.Host = "smtp.gmail.com";
smpt.Port = 587;
smpt.EnableSsl = true;
smpt.Credentials = new System.Net.NetworkCredential("test@gmail.com", "1234567890");
smpt.Send(msg);
}
catch (Exception ex)
{
}
即使我在代码中设置了凭据,我是否需要在我的 webconfig 文件中包含任何内容?