Sending email in .NET through Gmail

Instead of relying upon my host to send an email, I was considering sending out the email messages utilizing my Gmail account. The e-mails are individualized e-mails to the bands I use my show.

Is it feasible to do it?

2022-06-06 19:07:22
Source Share
Answers: 1

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

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 = "",
    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

Additionally most likely to the Google Account > Security web page and also consider the Signing in to Google > 2 - Step Verification setup.

  • If it is made it possible for, after that you need to create a password allowing.NET to bypass the 2 - Step Verification. To do this, click Signing in to Google > App passwords, select application = Mail, and also tool = Windows Computer, and also ultimately create the password. Make use of the created password in the fromPassword constant as opposed to your typical Gmail password
  • If it is impaired, after that you need to activate Less secure app access, which is not advised! So far better enable the 2 - Step confirmation
2022-06-06 19:13:00