【问题标题】:unable to connect to remote server c# while sending mail发送邮件时无法连接到远程服务器c#
【发布时间】: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


【解决方案1】:

您可以尝试将您的代码缩短为以下内容吗

 var smptClient = new SmtpClient("smtp.gmail.com", 587)
 {
     Credentials = new NetworkCredential("isavepluscom@gmail.com", "123456789"),
     EnableSsl = true
 };
  smptClient.Send("isavepluscom@gmail.com", "test@gmail.com", "Testing Email", "testing the email");

【讨论】:

  • 我知道你做了什么,但问题仍然存在,可能是我的服务器防火墙阻止了连接,我会尝试看看
  • 可能是这种情况,但是该代码确实适用于我在我们网络上使用我自己的帐户进行的最终测试。谢谢
【解决方案2】:

看起来像一个安全异常。确保打开 587 端口进行通信,如果是服务器,我将默认被阻止。

【讨论】:

    猜你喜欢
    • 2014-07-06
    • 1970-01-01
    • 2014-06-15
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多