【发布时间】:2013-08-06 07:14:19
【问题描述】:
我目前正在尝试通过 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 文件中包含任何内容?
【问题讨论】:
-
尝试用下面的 msg.To.Add("isavepluscom@gmail.com"); 替换这一行
msg.To.Add(new System.Net.Mail.MailAddress("isavepluscom@gmail.com"));;` 也用msg.From = new MailAddress("test@gmail.com");替换这一行msg.From = new System.Net.Mail.MailAddress("test@gmail.com"); -
不,这不起作用,它仍然给我错误:无法连接到远程服务器
-
Gmail期望在什么端口上接收它的凭据......?你也有一个名为test的gmail帐户以及那个确切的密码..?希望您用正确的凭据替换它 -
我确定是 587,是的,我将其替换为我自己的,出于安全考虑,我在此处放置了一个假的
-
看看我的回答,我刚刚用我自己的 Gmail 帐户/密码测试了这个。阅读起来更容易
标签: c# .net email io html-email