Created
May 23, 2018 12:05
-
-
Save h26k2/20504f81c525e0bda5bacd3eca7534e5 to your computer and use it in GitHub Desktop.
Just a demonstration of Sending emails in c# using Smtp
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
//These Two namespaces are required for SMTP | |
using System.Net; | |
using System.Net.Mail; | |
namespace Practice { | |
class SendEmails { | |
static void Main(string[] args) { | |
string myMail, personMail, msg, password,sub; | |
myMail = "write your email address here"; | |
password = "write your password here"; | |
personMail = "write recipient email address here"; | |
sub = "Email practice in c#"; | |
msg = "Just a demo message for demonstrating the SMTP in c#"; | |
try { | |
//creating instance of SMTP | |
SmtpClient emailClient = new SmtpClient("smtp.gmail.com", 587); | |
//adding my email credentials to this instance | |
emailClient.Credentials = new NetworkCredential(myMail, password); | |
//enabling SSL | |
emailClient.EnableSsl = true; | |
//sending email | |
emailClient.Send(myMail, personMail, sub, msg); | |
//displaying message to user that email has been sent | |
Console.WriteLine("Hurray! Email has been sent."); | |
} | |
catch(Exception e) { | |
Console.WriteLine("Error occured while performing the action...\n Error : " + e.Message); | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment