Question

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("your_____@gmail.com", "password");

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

        email.To.Add(new MailAddress("my____email@gmail.com"));
        email.From = new MailAddress("your_____@gmail.com");
        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}

Was it helpful?

Solution

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

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

OTHER TIPS

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?

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top