Created
April 16, 2017 11:47
-
-
Save SebastianGaud/3b5c6ae4bfd5fb234fc0f16ccb30ebb3 to your computer and use it in GitHub Desktop.
Send Email in C# through google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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