The .NET framework has built-in namespace for handling email settings, which is System.Net.Mail namespace. In the following example, I’ll use two classes from the System.Net.Mail namespace:
For email settings, we will use the MailMessage class and
For smtp settings and sending email, we will use the SmtpClient class
What is SMTP?
SMTP stands for Simple Mail Transfer Protocol. It provides set of communication settings that allows our software or web applications to transmit or send emails over the internet with configured smtp settings.
Required Gmail SMTP Settings:
Following are the list of SMTP settings that are mainly required to send emails:
SMTP Server/Host: It’s a server name. For example, smtp.gmail.com
SMTP Username: Your full email address. For example, yourname@gmail.com
SMTP Password: Your account password
SMTP port (TLS): 587
SMTP port (SSL): 465
SMTP TLS/SSL required: yes
SmtpClient:
This class allows the application to send email by using Simple Mail Transfer Protocol. Basic properties of this class needs to be assigned values to acheive this, ‘Port’, ‘Credentials, ‘SMTP server name’.
These are the list of SMTP Servers and its port numbers.
| Sl.No | Mail Server | SMTP Server( Host ) | Port Number |
| 1 | Gmail | smtp.gmail.com | 587 |
| 2 | Outlook | smtp.live.com | 587 |
| 3 | Yahoo Mail | smtp.mail.yahoo.com | 465 |
| 4 | Yahoo Mail Plus | plus.smtp.mail.yahoo.com | 465 |
| 5 | Hotmail | smtp.live.com | 465 |
| 6 | Office365.com | smtp.office365.com | 587 |
| 7 | zoho Mail | smtp.zoho.com | 465 |
in your program import System.Net.Mail and System.Net
public void sendEmail()
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
try
{
MailAddress fromAddress = new MailAddress(“yourmail@gmail.com”);
message.From = fromAddress;
message.To.Add(“receivermail@gmail.com”); //email id to whome you want to send
message.Subject = “Subject of your mail”;
message.IsBodyHtml = true;
message.Body = “This is the body of the mail that you want to send.”;
smtpClient.Host = “smtp.gmail.com”; // We use gmail as our smtp client
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(“yourmail@gmail.com”, “yourpassword”);
smtpClient.Send(message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
ClientScript.RegisterStartupScript(this.GetType(), null, “alert(‘Failed to send mail’)”);
}
}