简体   繁体   English

通过 gmail SMTP 服务器发送 Email (SSL)

[英]Sending an Email through gmail SMTP Server (SSL)

I know there's a lot of example on how to send an email using C# but I am really running into some issues and I can't make it to work.我知道有很多关于如何使用 C# 发送 email 的示例,但我确实遇到了一些问题,我无法让它工作。

I always get "Failure sending mail" error, Unable to connect to the remote server - No connection could be made because the active machine actively refused it (IP address here) .我总是收到"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)

What does this error mean?这个错误是什么意思? And how do I fix this?我该如何解决这个问题?

Any help will be much appreciated任何帮助都感激不尽

Here's the code that I've been using: (although I already did try a lot of things)这是我一直在使用的代码:(虽然我已经尝试了很多东西)

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)
{
}

Thanks!谢谢!

It sounds like you're running into issues connecting to the SMTP server.听起来您在连接到 SMTP 服务器时遇到了问题。

Make sure the firewall is open on port 25, and that you actually have an SMTP server running wherever you're trying to establish your connection.确保防火墙在端口 25 上打开,并且您实际上有一个 SMTP 服务器在您尝试建立连接的任何地方运行。

You're trying to establish an SSL connection to gmail on port 587 .您正在尝试在端口587上建立与gmail的 SSL 连接。 That port must be used with TLS, not SSL.该端口必须与 TLS 一起使用,而不是 SSL。

Use port 465 instead.请改用端口465

Also note that the Timeout property is expressed in milliseconds, so 3000 can be a little short, depending on your network.另请注意, Timeout属性以毫秒为单位,因此3000可能有点短,具体取决于您的网络。 Try using a more permissive value, like 30000 .尝试使用更宽松的值,例如30000

Also worth checking that your anti-virus is not blocking port 25, I have been caught by that before!还值得检查一下您的防病毒软件是否没有阻止端口 25,我之前已经被那个发现了!

Same for me in past few days,前几天我也是这样

Easy solution:简单的解决方案:

First check if everything is OK with Telnet , then try to do the same in C#.首先检查Telnet是否一切正常,然后尝试在 C# 中执行相同操作。 Here is a good intro: http://www.wikihow.com/Send-Email-Using-Telnet .这是一个很好的介绍: http://www.wikihow.com/Send-Email-Using-Telnet

Just beware that some servers use the EHLO command instead of HELO请注意,某些服务器使用EHLO命令而不是HELO

EDIT:编辑:

take a look at the way that you can connect to SMTP server of Google here看看你可以在这里连接到谷歌的 SMTP 服务器的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM