Skip to content

Instantly share code, notes, and snippets.

@SebastianGaud
Created April 16, 2017 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SebastianGaud/3b5c6ae4bfd5fb234fc0f16ccb30ebb3 to your computer and use it in GitHub Desktop.
Save SebastianGaud/3b5c6ae4bfd5fb234fc0f16ccb30ebb3 to your computer and use it in GitHub Desktop.
Send Email in C# through google
public static void CreateTestMessage4 ( string server )
{
MailAddress from = new MailAddress( "senderemail" );
MailAddress to = new MailAddress( "recipientemail" );
MailMessage message = new MailMessage( from , to );
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment( Environment.CurrentDirectory + "/file.pdf" );
var smtp = new SmtpClient
{
Host = "smtp.gmail.com" ,
Port = 587 ,
EnableSsl = true ,
DeliveryMethod = SmtpDeliveryMethod.Network ,
UseDefaultCredentials = false ,
Credentials = new NetworkCredential( from.Address , /*PassWord Here*/"aaa" )
};
message.Subject = "Using the SmtpClient class.";
message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
message.Attachments.Add( attachment );
SmtpClient client = smtp;
Console.WriteLine( "Sending an e-mail message to {0} by using SMTP host {1} port {2}." ,
to.ToString() , client.Host , client.Port );
try
{
client.Send( message );
}
catch ( Exception ex )
{
Console.WriteLine( "Exception caught in CreateTestMessage4(): {0}" ,
ex.ToString() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment