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("myemail@gmail.com", "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 follo开发者_如何学Pythonwing settings:
<smtp from="myemail@gmail.com" deliveryMethod="Network">
<network host="smtp.gmail.com"
userName="myemail@gmail.com"
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.
Your code looks fine. You say you tried Telneting. Were you actually able to send an email through telnet?
You could try removing
from="myemail@gmail.com" 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.).
精彩评论