Skip to content

Instantly share code, notes, and snippets.

@LaraSQP
Last active September 22, 2021 21:28
Show Gist options
  • Save LaraSQP/af0cd025243d9e3a4964f87b0ef6f7dd to your computer and use it in GitHub Desktop.
Save LaraSQP/af0cd025243d9e3a4964f87b0ef6f7dd to your computer and use it in GitHub Desktop.
// In addition to the code below, it is necessary to either:
// 1 - Enable "Less secure app access" in Google Account -> Security
// 2 - If using 2-Step Verification, go to Google Account -> Security -> Add passwords
// then select app = Mail and device = Windows Computer to generate a password
// An alternative to the above is to authorize access for the program as shown here:
// https://developers.google.com/gmail/api/quickstart/dotnet
using var msg = new MailMessage();
using var attachment = new Attachment( @"d:\icon.png" );
msg.From = new MailAddress( "[email protected]" );
msg.Subject = "A subject";
msg.Body = "Hello world";
msg.To.Add( "[email protected]" );
msg.Attachments.Add( attachment );
using var client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential( "[email protected]", "gmail-generated-password" );
try
{
client.Send( msg );
MessageBox.Show( "Email has been successfully sent." );
}
catch( Exception ex )
{
MessageBox.Show( "Unable to send email. Exception: " + ex.Message );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment