通过Gmail邮件服务器(SSL)发送电子邮件

时间:2011-05-05 14:16:16

标签: c# email email-integration

我知道有很多关于如何使用C#发送电子邮件的示例,但我真的遇到了一些问题,我无法让它发挥作用。

我总是收到"Failure sending mail"错误,Unable to connect to the remote server - No connection could be made because the active machine actively refused it (IP address here)

这个错误是什么意思?我该如何解决这个问题?

非常感谢任何帮助

这是我一直在使用的代码:(虽然我已经尝试了很多东西)

string SendersAddress = "test@gmail.com";
string ReceiversAddress = "test1@gmail.com";
const string SendersPassword = "test-pass-here";
const string subject = "Testing";
const string body = "Hi This Is my Mail From Gmail";
try
{
    SmtpClient smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(SendersAddress, SendersPassword),
            Timeout = 3000
        };
    MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
    smtp.Send(message);
}
catch (Exception ex)
{
}

谢谢!

4 个答案:

答案 0 :(得分:2)

听起来您遇到了连接到SMTP服务器的问题。

确保防火墙在端口25上打开,并确保在您尝试建立连接的任何地方运行SMTP服务器。

答案 1 :(得分:2)

您正尝试在端口587上建立与gmail的SSL连接。该端口必须与TLS一起使用,而不是SSL。

改为使用端口465

另请注意,Timeout属性以毫秒表示,因此3000可能会有点短,具体取决于您的网络。尝试使用更宽松的值,例如30000

答案 2 :(得分:1)

还值得检查一下你的防病毒软件是否阻塞了端口25,我之前已经被它抓住了!

答案 3 :(得分:0)

过去几天我也一样,

简易解决方案:

首先检查Telnet是否一切正常,然后尝试在C#中执行相同的操作。   这是一个很好的介绍:http://www.wikihow.com/Send-Email-Using-Telnet

请注意,某些服务器使用EHLO命令而不是HELO

编辑:

查看您可以连接到Google here

的SMTP服务器的方式
相关问题