cookieChoices = {};

Friday 20 September 2013

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 How to send emails in asp.net | How to set Smtp setting in web.config file to send email in asp.net and Send emails in asp.net using Gmail | How to set Smtp setting in web.config file to send emails in asp.net using Gmail 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 application to understand:

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>

C#.NET Code to send emails in asp.net using Gmail 
 
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, stringgmailPassword)
    {
        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;
        }
    }

No comments:

Post a Comment