开发者

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.

I always get "Failure sending mail" error, Unable to connect to the remote server - No c开发者_运维技巧onnection 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.

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.


You're trying to establish an SSL connection to gmail on port 587. That port must be used with TLS, not SSL.

Use port 465 instead.

Also note that the Timeout property is expressed in milliseconds, so 3000 can be a little short, depending on your network. Try using a more permissive value, like 30000.


Also worth checking that your anti-virus is not blocking port 25, I have been caught by that before!


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#. Here is a good intro : http://www.wikihow.com/Send-Email-Using-Telnet.

Just beware that some servers use the EHLO command instead of HELO

EDIT:

take a look at the way that you can connect to SMTP server of Google here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜