views:

261

answers:

3

I am trying to create a web application which upon entering your email address and message , sends an email with this information from the email address.

I used this:

try { NetworkCredential login = new NetworkCredential("[email protected]", "password");

        System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();

        email.To.Add(new MailAddress("[email protected]"));
        email.From = new MailAddress("[email protected]");
        email.Subject = "Question";

        email.Body = question;

        SmtpClient client = new SmtpClient("smtp.gmail.com");
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = login;
        client.Send(email);
    }
    catch
    {
    }

But its giving me an SMTP error.

"Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 209.85.129.111 (209.85.129.111:25), connect error 10051" System.Exception {System.Net.Mail.SmtpException}

+3  A: 

To send through your gmail account, you need to connect to port 587:

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
RickNZ
THANK YOU VERY MUCH! :D
Lily
A: 

You do not need to specify port 587 - the code works without it. I have successfully sent and received e-mail using:

SmtpClient client = new SmtpClient("smtp.gmail.com");

If you look at the error closely, it says "Cannot connect to SMTP server" and error 10051 means the network is unreachable. Do you have a firewall blocking port 587?

Andrei Drynov
A: 

Gmail uses port 465 and the erros show port 25 try using 465 port

http://mail.google.com/support/bin/answer.py?answer=76147

Diego Mendes