Home Asp.net How to Send Email using Gmail SMTP Server in Asp.net C# Vb.net?

How to Send Email using Gmail SMTP Server in Asp.net C# Vb.net?

5
4

Now here in this tutorial, I’ll explain how to Send Email Using Gmail Smtp settings in asp.net using C# and vb.net.

In my previous tutorials, I’d explained how to send asp.net gridview in mail, how to send HTML web page as email and more cracking tutorials on Repeater, Gridivew, Asp.net, JavaScript, jQuery here.

 

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

I guess you are aware that mails are sent via an SMTP server, so we need to integrate smtp settings such as SMTP Host, Port, Credentials etc. If you don’t know about such things don’t worry, I’ll cover all these things in this tutorial.

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
Note: If you have your own email provider and you want to use that details then you can use that too.

Now we have all required details available, so we can go ahead and implement code to test mail using Gmail smtp

HTML Markup Code To Send Mail – [.aspx]

Following is the complete HTML markup code for .aspx page shown as below:

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head id=”Head1″ runat=”server”>
<title>Send test mail using smtp settings in asp.net</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td>
<h4>Send Email Using Gmail SMTP server settings in asp.net</h4>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID=”txtToEmail” runat=”server” Width=”250″ Height=”28″
placeholder=”To email: example@example.com”></asp:TextBox>
<asp:RequiredFieldValidator ID=”rfvToEmail” runat=”server” ForeColor=”Red”
ControlToValidate=”txtToEmail” ErrorMessage=”Required” Display=”Dynamic”>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID=”revToEmail” runat=”server”
ControlToValidate=”txtToEmail” ForeColor=”Red” ErrorMessage=”Invalid Email”
ValidationExpression=”\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”>
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>&nbsp;&nbsp;</td>
</tr>
<tr>
<td>
<asp:TextBox ID=”txtMessage” runat=”server” placeholder=”Your body message”
Width=”250″ Height=”150″ TextMode=”MultiLine”></asp:TextBox>
<asp:RequiredFieldValidator ID=”rfvMessage” runat=”server”
ControlToValidate=”txtMessage” ForeColor=”Red” ErrorMessage=”Required”>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>&nbsp;&nbsp;</td>
</tr>
<tr>
<td>
<asp:Button ID=”btnSend” runat=”server” Text=”Send Mail!” OnClick=”btnSend_Click” />
</td>
</tr>
<tr>
<td>&nbsp;&nbsp;</td>
</tr>
<tr>
<td>
<asp:Label ID=”lblMsg” runat=”server”></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Following is the code that we need to use for sending emails, choose your language from below.

Send Email Using Gmail SMTP Settings Sample Code – [C#]

Add the following namespace that is required for sending emails:

using System.Net.Mail;

Now following is the code that we can use to send emails using MailMessage class:

protected void Page_Load(object sender, EventArgs e)
{
}protected void btnSend_Click(object sender, EventArgs e)
{
try
{
string Subject = “This is test mail using smtp settings”,
Body = txtMessage.Text.Trim(),
ToEmail = txtToEmail.Text.Trim();string SMTPUser = “yourname@gmail.com”, SMTPPassword = “yourpassword”;//Now instantiate a new instance of MailMessage
MailMessage mail = new MailMessage();//set the sender address of the mail message
mail.From = new MailAddress(SMTPUser, “AspnetO”);//set the recepient addresses of the mail message
mail.To.Add(ToEmail);

//set the subject of the mail message
mail.Subject = Subject;

//set the body of the mail message
mail.Body = Body;

//leave as it is even if you are not sending HTML message
mail.IsBodyHtml = true;

//set the priority of the mail message to normal
mail.Priority = MailPriority.Normal;

//instantiate a new instance of SmtpClient
SmtpClient smtp = new SmtpClient();

//if you are using your smtp server, then change your host like “smtp.yourdomain.com”
smtp.Host = “smtp.gmail.com”;

//chnage your port for your host
smtp.Port = 25; //or you can also use port# 587

//provide smtp credentials to authenticate to your account
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);

//if you are using secure authentication using SSL/TLS then “true” else “false”
smtp.EnableSsl = true;

smtp.Send(mail);

lblMsg.Text = “Success: Mail sent successfully!”;
lblMsg.ForeColor = System.Drawing.Color.Green;
}
catch (SmtpException ex)
{
//catched smtp exception
lblMsg.Text = “SMTP Exception: “ + ex.Message.ToString();
lblMsg.ForeColor = System.Drawing.Color.Red;
}
catch (Exception ex)
{
lblMsg.Text = “Error: “ + ex.Message.ToString();
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}

Send Email Using Gmail SMTP Settings Sample Code – [Vb.net]

Add the following namespace that is required for sending emails:

Imports System.Net.Mail

Now following is the code that we can use to send emails using MailMessage class:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End SubProtected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim Subject As String = “This is test mail using smtp settings”,
Body As String = txtMessage.Text.Trim(), ToEmail As String = txtToEmail.Text.Trim()Dim SMTPUser As String = “yourname@gmail.com”,
SMTPPassword As String = “yourpassword”‘Now instantiate a new instance of MailMessage
Dim mail As New MailMessage()‘set the sender address of the mail message
mail.From = New MailAddress(SMTPUser, “AspnetO”)‘set the recepient addresses of the mail message
mail.To.Add(ToEmail)

‘set the subject of the mail message
mail.Subject = Subject

‘set the body of the mail message
mail.Body = Body

‘leave as it is even if you are not sending HTML message
mail.IsBodyHtml = True

‘set the priority of the mail message to normal
mail.Priority = MailPriority.Normal

‘instantiate a new instance of SmtpClient
Dim smtp As New SmtpClient()

‘if you are using your smtp server, then change your host like “smtp.yourdomain.com”
smtp.Host = “smtp.gmail.com”

‘chnage your port for your host
smtp.Port = 25 ‘or you can also use port# 587

‘provide smtp credentials to authenticate to your account
smtp.Credentials = New System.Net.NetworkCredential(SMTPUser, SMTPPassword)

‘if you are using secure authentication using SSL/TLS then “true” else “false”
smtp.EnableSsl = True

smtp.Send(mail)

lblMsg.Text = “Success: Mail sent successfully!”
lblMsg.ForeColor = System.Drawing.Color.Green
Catch ex As SmtpException
‘catched smtp exception
lblMsg.Text = “SMTP Exception: “ & ex.Message.ToString()
lblMsg.ForeColor = System.Drawing.Color.Red
Catch ex As Exception
lblMsg.Text = “Error: “ & ex.Message.ToString()
lblMsg.ForeColor = System.Drawing.Color.Red
End Try
End Sub

Example Result

send email using gmail smtp

Download Example

[wpdm_package id=’5427′]

4 COMMENTS

  1. Dear Mayank,

    Hi, i am .Net Developer. the current task i have is i need to read the unread mail from my gmail inbox.

    i am able to read all mail present in the inbox but i need to filter with only unread mail.

    Appreciate your support

    Thanks and Regards

    Prasad Joshi

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!