views:

515

answers:

1

I am trying to send a basic email through Google Apps/Gmail using C# (System.Net.Mail/Framework 4) and I am having trouble doing so.

I am receiving the following exception: "The operation has timed out."

My code is below:

            //Create the mail message
            MailMessage mail = new MailMessage();

            //Set the addresses
            mail.From = new MailAddress("[email protected]", "My Name");
            mail.To.Add(Email);

            //Set the subject and bodycontent
            mail.Subject = "Email Testing";
            mail.Body = "This is the body of the email";

            //Send the message using the SmtpClient
            SmtpClient smtp = new SmtpClient();
            smtp.EnableSsl = true;
            smtp.Send(mail);

My web.config has the following settings:

      <smtp from="[email protected]" deliveryMethod="Network">
          <network host="smtp.gmail.com" 
              userName="[email protected]"
              password="password" 
              port="587" />
      </smtp>

During my troubleshooting I have tried:

  • Using my personal gmail address as well as another from a domain hosted through Google Apps.
  • Using ports 25, 465, and 587
  • Hard coding the config settings in the c# code instead of using the web.config
  • Sending and telneting from multiple network locations to ensure the firewall/ISP was not blocking it
  • Ensured that POP was enabled in the GMail settings (according to Google this should turn on the ability to send using SMTP)
  • Changing the send from and replyTo address to ensure they match the account (apparently a GMail necessity).

I am able to send and receive email fine through the GMail interface for both of my email accounts. I have also tried the settings and solutions offered in Question # 757987 to no avail.

+1  A: 

Your code looks fine. You say you tried Telneting. Were you actually able to send an email through telnet?

You could try removing

from="[email protected]" deliveryMethod="Network"

from your smtp tag since they aren't necessary for what you're doing.

EDIT:

Telnet will normally allow you to connect to the smtp server and send 16 (?) bytes of data before it will kick you out. If you aren't getting responses like these, then this is likely the case.

Try opening up port 587 on your firewall and see if you can actually interact with the smtp server via telnet (EHLO, ECHO, etc.).

Joel Potter
Thank you for the suggestions. I have tried remove the "from" and "deliverymethod" from the smtp tag and it is still timing out.When I mentioned I tried telneting in my initial question...I tried connecting and it connected, but when I try to send an email using the instructions I found at "http://www.wikihow.com/Send-Email-Using-Telnet" it kicks me out after the first command.
NateReid
Sounds like your telnet isn't actually connected then, in which case you probably have a network problem. Try opening up the 587 port on your firewall
Joel Potter
NateReid