Sending emails (particularly with gmail and google apps for domains)


/ Published in: C#
Save to your folder(s)

For both gmail and gApps, use smtp.gmail.com on port 25 with SSL and NetworkCredentials.

Neither can send email to their own address, but they can send to other addresses.

[NLog](http://nlog-project.org/) has [an example](http://nlog-project.org/wiki/Using_NLog_with_GMail) of how to automatically send emails in order to log info from an app.


Copy this code and paste it in your HTML
  1. string host = "smtp.gmail.com";
  2. int port;
  3. //port = 587; // with TLS
  4. //port = 465; // with SSL
  5. port = 25; // normal - works for my gmail account with enableSSL
  6. string fromEmail = "[email protected]";
  7. var fromAddress = new MailAddress(fromEmail);
  8. var toAddress = new MailAddress("[email protected]");
  9. MailMessage message = new MailMessage(fromAddress, toAddress)
  10. {
  11. Subject = "subject",
  12. Body = "body",
  13. };
  14. var smtp = new SmtpClient(host, port) {
  15. Credentials=new NetworkCredential(fromEmail, "password"),
  16. EnableSsl = true,
  17. Timeout = 30000,
  18. };
  19.  
  20. try {
  21. smtp.Send(message);
  22. }
  23. catch(SmtpException se) {
  24. //log
  25. }
  26. message.Dispose();

URL: http://www.google.com/support/forum/p/Google+Apps/thread?tid=3abf482d7d7e24d3&hl=en&fid=3abf482d7d7e24d3000484c10943499b

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.