In most of our website projects there is a need to send emails to website administrators, users, newsletter subscribers etc. In asp .net we can send emails using System.Net.Mail class. However this class needs SMTP server settings to send emails. To provide necessary SMTP settings we can either use our own SMTP server or we can use SMTP server of third party Email service providers like Rediffmail, Gmail, Hotmail, Yahoo etc. In this article we will see how to send emails using asp .net and email service providers like Rediffmail, Gmail, Hotmail, Yahoo etc. We will also see list of top 10 email providers we can use to send emails with asp .net.
Note: This method requires internet connection to send emails, you can also send emails without internet connection in asp .net using SmtpDeliveryMethod.SpecifiedPickupDirectory. For more information refer this tutorial.
Step1: Create .aspx markup:
Step2: Add Namespace.
using System.Net.Mail;
using System.Net;
Step3: Code Behind.
//--- Button Click to send emails.
protected void btnSendEmail_Click(object sender, EventArgs e)
{
using (MailMessage mm = new MailMessage())
{
mm.From = new MailAddress("YourEmailAddress"); //--- Email address of the sender
mm.To.Add(txtEmailTo.Text); //---- Email address of the recipient.
mm.Subject = txtEmailSubject.Text; //---- Subject of email.
mm.Body = txtEmailBody.Text; //---- Content of email.
mm.IsBodyHtml = false; //---- To specify wether email body contains HTML tags or not.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //---- SMTP Host Details.
smtp.EnableSsl = true; //---- Specify whether host accepts SSL Connections or not.
NetworkCredential NetworkCred = new NetworkCredential("YourEmailAddress", "YourPassword");
//---Your Email and password
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587; //---- SMTP Server port number. This varies from host to host.
smtp.Send(mm);
//==== Show confirmation message.
lblConfirmation.Text = "Congratulations, Your email successfully sent";
//---- Clear form fields.
txtEmailBody.Text = string.Empty;
txtEmailSubject.Text = string.Empty;
txtEmailTo.Text = string.Empty;
}
}
From above example you can see how easily we can use power of asp .net and email service providers to send emails. Here is a list of 10 email service providers you can use to send emails.
Sno |
Server Name |
SMTP Address |
Port |
SSL |
1 |
GMail |
smtp.gmail.com |
587 |
Yes |
2 |
Hotmail |
smtp.live.com |
587 |
Yes |
3 |
Yahoo |
smtp.mail.yahoo.com |
587 |
Yes |
4 |
Rediffmail |
smtp.rediffmail.com |
587 |
Yes |
5 |
Outlook |
smtp.live.com |
587 |
Yes |
6 |
AOL |
smtp.aol.com |
587 |
Yes |
7 |
AIM |
smtp.aim.com |
587 |
Yes |
8 |
Zoho Mail |
smtp.zoho.com |
465 |
Yes |
9 |
Mail.com |
smtp.mail.com |
587 |
Yes |
10 |
Sify.com |
smtp.sify.com |
587 |
Yes |
To use these email service providers you have to just do changes in :
- smtp.Host
- smpt.EnableSSL
- NetworkCredentials
- smtp.Port
Example: Suppose you want to send email using your rediff mail and asp .net you need to change these settings only:
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.rediffmail.com"; //---- SMTP Host Details.
smtp.EnableSsl = true; //---- Specify whether host accepts SSL Connections or not.
NetworkCredential NetworkCred = new NetworkCredential("YourRediffMailId", "YourRediffMailPassword");
//---Your Email and password
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587; //---- SMTP Server port number. This varies from host to host.
smtp.Send(mm);
Final Output: