Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email in .NET through Gmail

Instead of relying on my host to send an email, I was thinking of sending the email messages using my Gmail account. The emails are personalized emails to the bands I play on my show.

Is it possible to do it?

like image 728
Mike Wills Avatar asked Aug 28 '08 13:08

Mike Wills


People also ask

Can you send an email to a .net address?

NET and . NET Core come with built-in support for sending emails through the System. Net.Mail namespace. While it might seem like the easy choice, you will need an SMTP server for this to work.

Can I use Gmail as an SMTP server?

Use the Gmail SMTP serverIf you connect using SSL or TLS, you can send mail to anyone inside or outside of your organization using smtp.gmail.com as your server. This option requires you to authenticate with your Gmail or Google Workspace account and passwords.


2 Answers

Be sure to use System.Net.Mail, not the deprecated System.Web.Mail. Doing SSL with System.Web.Mail is a gross mess of hacky extensions.

using System.Net; using System.Net.Mail;  var fromAddress = new MailAddress("[email protected]", "From Name"); var toAddress = new MailAddress("[email protected]", "To Name"); const string fromPassword = "fromPassword"; const string subject = "Subject"; const string body = "Body";  var smtp = new SmtpClient {     Host = "smtp.gmail.com",     Port = 587,     EnableSsl = true,     DeliveryMethod = SmtpDeliveryMethod.Network,     UseDefaultCredentials = false,     Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) {     Subject = subject,     Body = body }) {     smtp.Send(message); } 

Additionally go to the Google Account > Security page and look at the Signing in to Google > 2-Step Verification setting.

  • If it is enabled, then you have to generate a password allowing .NET to bypass the 2-Step Verification. To do this, click on Signing in to Google > App passwords, select app = Mail, and device = Windows Computer, and finally generate the password. Use the generated password in the fromPassword constant instead of your standard Gmail password.
  • If it is disabled, then you have to turn on Less secure app access, which is not recommended! So better enable the 2-Step verification.
like image 123
Domenic Avatar answered Oct 13 '22 10:10

Domenic


The above answer doesn't work. You have to set DeliveryMethod = SmtpDeliveryMethod.Network or it will come back with a "client was not authenticated" error. Also it's always a good idea to put a timeout.

Revised code:

using System.Net.Mail; using System.Net;  var fromAddress = new MailAddress("[email protected]", "From Name"); var toAddress = new MailAddress("[email protected]", "To Name"); const string fromPassword = "password"; const string subject = "test"; const string body = "Hey now!!";  var smtp = new SmtpClient {     Host = "smtp.gmail.com",     Port = 587,     EnableSsl = true,     DeliveryMethod = SmtpDeliveryMethod.Network,     Credentials = new NetworkCredential(fromAddress.Address, fromPassword),     Timeout = 20000 }; using (var message = new MailMessage(fromAddress, toAddress) {     Subject = subject,     Body = body }) {     smtp.Send(message); } 
like image 44
Donny V. Avatar answered Oct 13 '22 10:10

Donny V.