C# send email using System.Net SMTP Client

C# send email using System.Net SMTP Client

  Many times while developing application you come across the requirement, where you need to send emails to users using your application.
Here I will use Gmail account for sending emails. First you need to open Gmail account and configure it's settings to allow
third party clients send emails or receive emails. You can refer how to configure Gmail account by visiting here: Gmail configuration
send email using asp.net and gmail smtp

 
using System.Net.Mail;
using System.Net;
   It uses above namespace. System.Net will be used for network credentials and System.Net.Mail
   will be used for Sending and creating mail message object.
private void SendEmail(string fromEmailAddress, string toEmailAddress)
{
private void SendEmail(UserProfileInfo u)
{
NetworkCredential loginInfo = new NetworkCredential(fromEmailAddress, "Password_of_gmail");
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromEmailAddress);
msg.To.Add(new MailAddress(toEmailAddress));
msg.Subject = "Your email subject";
msg.Body = "Hi, <br/> This is test email <br/><br/> please ignore this email.";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);        
}

Description:


Network Credentilas:


Network credential instance accepts username and password. Pass your Gmail credentials to constructor of Network credential.
				NetworkCredential loginInfo = new NetworkCredential(fromEmailAddress, "Password_of_gmail");
	

Mail Message Object:

Create instance of mail message and configure is From Email address, To Email address, Subject and body of email.

		MailMessage msg = new MailMessage();
	msg.From = new MailAddress(fromEmailAddress);
	msg.To.Add(new MailAddress(toEmailAddress));
	msg.Subject = "Your email subject";
	msg.Body = "Hi, <br/> This is test email <br/><br/> please ignore this email.";
	msg.IsBodyHtml = true;	
	

HTML body:
You can send email as HTML body or plain text. To send email as HTML body set mailmessage object ISBodyHtml = true.

				msg.IsBodyHtml = true;
	
SMTP Client setup:

Create instance of SMTP client and use Smtp.gmail.comas SMTP address


		SmtpClient client = new SmtpClient("smtp.gmail.com");
	client.EnableSsl = true;
	client.UseDefaultCredentials = false;
	client.Credentials = loginInfo;
	client.Send(msg); 	
	

EnableSSL to use secure connection with Gmail SMTP.

		client.Credentials = loginInfo;		
		

Tell SMTP client about your network credentials.

		client.Send(msg); 	
		
Pass mail message object to send method of SMtp client instance.

It will send email to destination.

Note:

You may requre to temporarily disbale the forewall. As it may block your application request to SMTP of gmail server.

Tags:

Gridview select emailTemporary disposable emailC-Send-emailYahoo email settingsSend EmailRead pop3 email attachments componentCheck Email AddressCreate contact us formc# send SMS (Text)C# WebClientASP.net read email hotmail

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.