Send Email via Google Apps in C#

Posted by NateReid on Stack Overflow See other posts from Stack Overflow or by NateReid
Published on 2010-03-31T13:31:03Z Indexed on 2010/03/31 13:33 UTC
Read the original article Hit count: 780

Filed under:
|
|

Hello,

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" password="password" port="587" userName="[email protected]" />
      </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.

Any suggestions would be greatly appreciated. Thanks all.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET