Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email via Google Apps in C#

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.

like image 872
NateReid Avatar asked Mar 31 '10 13:03

NateReid


1 Answers

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.).

like image 139
Joel Avatar answered Oct 06 '22 00:10

Joel