Sending mail from gmail SMTP C# Connection Timeout

30,631

Solution 1

You need to tell the SmtpClient what settings to use. It does not automatically read this information from the Web.Config file.

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);
smtp.Credentials = new NetworkCredential("[email protected]", "***");
smtp.EnableSsl = true;
smtp.Send(mail);

Solution 2

gmail requires authentication:

Outgoing Mail (SMTP) Server
requires TLS or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465

so what i did is

var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential("[email protected]", "mypwd"),
            EnableSsl = true
        };
        client.Send("[email protected]", "[email protected]", "Welcome to Writely", "Test content");

Solution 3

I had the exact same problem and it's resolved after switching the port number from 465 to 587.

I had the problem on "email confirmation", "password recovery", and "sending email" and now all 3 problems are resolved :).

I know it's a pretty old post, but I usually use the existing posts to find answers instead of asking for new questions.

Thank you all for all your helps.

Solution 4

As I have already answered here.

This problem can also be caused by a security configuration in you gmail account.

The correct port is 587, but to authenticate you need to allow access from less secure apps in your gmail account. Try it here

It worked for me, hope it helps..

Share:
30,631
Bernice
Author by

Bernice

Mostly interested in web development and Java. life motto: while(!successful) { tryAgain(); }

Updated on July 09, 2022

Comments

  • Bernice
    Bernice almost 2 years

    I have been trying to send an email via C# from a gmail account for account registration for my website.

    I have tried several ways however the same exception continues to pop up: System.Net.Mail.Smtp Exception - Connection has timed out.

    This is what I inluded in my Web.config file:

    <system.net>
        <mailSettings>
            <smtp deliveryMethod="Network" 
                  from="Writely &lt;[email protected]&gt;">
                <network host="smtp.gmail.com" 
                         port="465" 
                         enableSsl="true" 
                         defaultCredentials="false" 
                         userName="[email protected]" 
                         password="******" />
            </smtp>
        </mailSettings>
    </system.net>
    

    where writely is the name of my website, and [email protected] is the account I wish to send an email from.

    Then in my Account Controller when I connect with my database and save the user in my table, I am creating my MailMessage object and attempting to same the mail by:

    using (DBConnection conn = new DBConnection())
    {
           conn.UserInfoes.Add(userInfo);
           conn.SaveChanges();
    
           MailMessage mail = new MailMessage();
           mail.From = new MailAddress("[email protected]");
           mail.To.Add("[email protected]");
           mail.Subject = "Welcome to Writely";
           mail.Body = "Test content";
    
           SmtpClient smtp = new SmtpClient();
           smtp.Send(mail);
    }
    

    Am I missing something or doing something wrong? I read that this is the good way to do this in some other question on stack overflow so I really don't know what's the problem here.

    Thanks for your help :)

  • Bernice
    Bernice about 11 years
    Isn't this the line where I specify the server hostname? network host="smtp.gmail.com" in web.config?
  • Jason Watkins
    Jason Watkins about 11 years
    @Bernice Sorry, let me rephrase. The information in the Web.Config is not automatically used by the SmtpClient class. See the code in my updated answer.
  • Bernice
    Bernice about 11 years
    oh ok thanks :) .. and does google use port 587 or 465 ? cause I am seeing different port numbers in each question over the internet. Do I need to change the port in web.config to 587 too?
  • Jason Watkins
    Jason Watkins about 11 years
    @Bernice The port you use depends on whether or not you are using SSL. You should use 465 if you are using SSL, and 587 for plain text. Apparently GMail is pretty fault tolerant though, since I confused my ports in the example.
  • HasanG
    HasanG over 10 years
    @Jason Watkins If it is not used, why this setting can be defined in web.config? What is it for?
  • Jason Watkins
    Jason Watkins over 10 years
    You can define arbitrary data in Web.config; it's just an XML file.
  • Mark
    Mark over 8 years
    It supposedly is used if the DefaultCredentials is set to true, and the constructor is called without parameters: weblogs.asp.net/scottgu/432854 (I should note I'm currently not having success with this however)
  • Talon
    Talon almost 7 years
    This information is incorrect. Instantiating the SmtpClient will automatically use the details set in the system.net/mailSettings/smtp section of your app.config or web.config. It is simple to test, put a breakpoint on the smtp.Send line and inspect the SmtpClient object. You will see all the information is there.