Jerald
Jerald

Reputation: 199

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 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 = "[email protected]";
string ReceiversAddress = "[email protected]";
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!

Upvotes: 4

Views: 17105

Answers (4)

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

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

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263117

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.

Upvotes: 2

Colin Desmond
Colin Desmond

Reputation: 4854

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

Upvotes: 1

Rex Morgan
Rex Morgan

Reputation: 3029

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.

Upvotes: 2

Related Questions