How to send emails in asp.net using Gmail

Introduction: Sometimes it is required to send emails from our website to other person.It’s very easy.In my previous article i explained How to send mail with multiple attachments in asp.net with C#,Vb.Net and Send email to multiple users based on CheckBox selection inside GridView and Create registration form and send confirmation email to new registered users in asp.net | Activate or approve user account by activation link in email address and How to send emails in asp.net | How to set Smtp setting in web.config file to send email in asp.net 
Now in this article i am going to explain how you can send email using your GMAIL account credentials i.e. gmail email id and password in asp.net.Just follow the article.

Implementation: Let's create an demo web page to understand how to send email in asp.net using gmail credentials:

  • In the design page (.aspx) design the page as:

<table style="width:100%;">
    <tr>
        <td>
            Send To(Email Address)</td>
        <td>
            <asp:TextBox ID="txtEmailId" runat="server" Columns="60"></asp:TextBox>
            </td>
    </tr>
    <tr>
        <td>
            Subject</td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server" Columns="60"></asp:TextBox>
            </td>
    </tr>
    <tr>
        <td>
            Body</td>
        <td>
            <asp:TextBox ID="txtBody" runat="server" Columns="60" Rows="5"
                TextMode="MultiLine"></asp:TextBox>
            </td>
    </tr>
    <tr>
        <td>
             </td>
        <td>
            <asp:Button ID="btnSendEmail" runat="server" onclick="btnSendEmail_Click"
                Text="Send Email" />
            </td>
    </tr>  
</table>

Asp.Net C# Code to send emails in asp.net using Gmail  

First of all Include following namespaces:

using System.Configuration;
using System.Net;
using System.Net.Mail;

  • Now in the code behind file(.aspx.cs) write the code to send Email on send mail button as:
protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        if (SendEmailUsingGmail("YourGmailId@gmail.com", txtEmailId.Text.Trim(), txtSubject.Text.Trim(), txtBody.Text.Trim(), "YourGmailPassword"))
        {
            Response.Write("Mail send");
        }
        else
        {
            Response.Write("Error in sending mail");
        }
    }

    private static Boolean SendEmailUsingGmail(string fromEmailAddress, string toEmailAddress, string subject, string messageBody, string gmailPassword)
    {
        try
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new NetworkCredential(fromEmailAddress, gmailPassword);
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;

            MailMessage message = new MailMessage();
            message.From = new MailAddress(fromEmailAddress);
            message.To.Add(toEmailAddress);
            message.Subject = subject;
            message.Body = messageBody;
            smtp.Send(message);
            return true;
        }
        catch
        {
            return false;
        }
    }

Asp.net VB Code to send emails in asp.net using Gmail

Include following namespaces:

imports System.Configuration
imports System.Net
imports System.Net.Mail
  • Now in the code behind file(.aspx.vb) write the code to send Email on send mail button as:
Protected Sub btnSendEmail_Click(sender As Object, e As EventArgs)
                    If SendEmailUsingGmail("YourGmailId@gmail.com", txtEmailId.Text.Trim(), txtSubject.Text.Trim(), txtBody.Text.Trim(), "YourGmailPassword") Then
                                         Response.Write("Mail send")
                    Else
                                         Response.Write("Error in sending mail")
                    End If
End Sub 

Private Shared Function SendEmailUsingGmail(fromEmailAddress As String, toEmailAddress As String, subject As String, messageBody As String, gmailPassword As String) As [Boolean]
                    Try
                                         Dim smtp As New SmtpClient()
                                         smtp.Credentials = New NetworkCredential(fromEmailAddress, gmailPassword)
                                         smtp.Port = 587
                                         smtp.Host = "smtp.gmail.com"
                                         smtp.EnableSsl = True

                                         Dim message As New MailMessage()
                                         message.From = New MailAddress(fromEmailAddress)
                                         message.[To].Add(toEmailAddress)
                                         message.Subject = subject
                                         message.Body = messageBody
                                         smtp.Send(message)
                                         Return True
                    Catch
                                         Return False
                    End Try
End Function

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Previous
Next Post »

10 comments

Click here for comments
Anonymous
admin
August 05, 2013 ×

This is very good.

Reply
avatar
August 08, 2013 ×

Thanks for the appreciation.stay tuned for more technical updates..

Reply
avatar
Unknown
admin
October 14, 2013 ×

hello sir,
i am using already mail sending functionality using gmail like ur post but now its not working in all my project. its throw error like due to security reason not able to send message. sir, plz resolved this issue, i hardly need this

Reply
avatar
October 14, 2013 ×

Hi ajay, please mention the exact error that you are facing while sending email..i will definitely help you sort out the error..

Reply
avatar
Unknown
admin
October 15, 2013 ×

sir,
i also try to use ur above code for sending mail using gmail but it not send it display "Error in sending mail" else part of function

Reply
avatar
October 16, 2013 ×

Have you tried to debug your code? this will let you know the exact error/exception you are facing..check and notify me..then i will give you the appropriate solution to your problem..

Reply
avatar
Anonymous
admin
November 25, 2013 ×

Sir please help in sending mail

Reply
avatar
November 25, 2013 ×

Are you getting any issue in sending email?

Reply
avatar
Unknown
admin
April 25, 2014 ×

i tried your code and while debuging i get this error!!!
Control 'txtEmailId' of type 'TextBox' must be placed inside a form tag with runat=server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'txtEmailId' of type 'TextBox' must be placed inside a form tag with runat=server.

Reply
avatar
April 26, 2014 ×

Hi..i think you have not placed the table inside the form tag of the body tag while designing default.aspx page..

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..