Friday, August 27, 2010

Asp.net - Send email from gmail by using Asp.net

You can use gmail account to send email from asp.net.This is not straight forward like a simple SMTP email.

Let see how to send email from gmail by using asp.net

Step 1

Create a web Project.

Step 2
Create a following static class on app_code folder.This is following class used for the sending email from gmail account.



using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mail;

/// <summary>
/// Send Mail from Gamil Account from ASP.net
/// </summary>
public static class GMail
{
    public static Boolean SendGMail(
        String GmailMailID,
        String GmailPassword,
        String To,
        String Subject,
        String Body,
        System.Web.Mail.MailFormat Format,
        String AttachmentPath)
    {
        Boolean Flag = false; 
        try
        {
            System.Web.Mail.MailMessage MyGMail = new System.Web.Mail.MailMessage();
            MyGMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            MyGMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            MyGMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");
           
            MyGMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
           
            MyGMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                GmailMailID);
            MyGMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                 GmailPassword);
            MyGMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 "true");

            MyGMail.From = GmailMailID;
            MyGMail.To = To;
            MyGMail.Subject = Subject;
            MyGMail.BodyFormat = Format;
            MyGMail.Body = Body; 

            if (AttachmentPath.Trim() != String.Empty)
            {
                MailAttachment MAttachment = new MailAttachment(AttachmentPath);
                MyGMail.Attachments.Add(MAttachment);
                MyGMail.Priority = System.Web.Mail.MailPriority.High;
            }

            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(MyGMail);

            Flag = true; 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }

        return Flag; 
    }
}


Step 3

Create a form on .aspx page.it is look like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <h2> Send Mail</h2>
    <table>
    <tr>
    <td style="text-align: right">Gmail Id : </td><td style="text-align: left"><asp:TextBox ID="txtGmailId" runat="server" Width="200px"></asp:TextBox></td>
    </tr>
    <tr>
    <td style="text-align: right">Password : </td><td style="text-align: left"><asp:TextBox ID="txtPassword" TextMode="Password" runat="server" Width="200px"></asp:TextBox></td>
    </tr>
    <tr>
    <td style="text-align: right">To : </td><td style="text-align: left"><asp:TextBox ID="txtTo" runat="server" Width="200px"></asp:TextBox></td>
    </tr>
    <tr>
    <td style="text-align: right">Subject : </td><td style="text-align: left"><asp:TextBox ID="txtSubject" runat="server" Width="200px"></asp:TextBox></td>
    </tr>
    <tr>
    <td style="text-align: right;vertical-align:top;">Message : </td><td><asp:TextBox  ID="txtBody" 
                        runat="server" TextMode="MultiLine" Height="100px" Width="300px"></asp:TextBox></td>
    </tr>
    <tr>
    <td></td>
    <td align="center">
        <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" Width="100px" /></td>
    </tr>
    </table>
        <asp:Label ID="lblError" ForeColor="red" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Step 4

Call SendGMail function on sendbutton click event,it is look like this

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Net;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            GMail.SendGMail(txtGmailId.Text.Trim() + "@gmail.com", txtPassword.Text.Trim(), txtTo.Text.Trim(), txtSubject.Text.Trim(),txtBody.Text.Trim(),System.Web.Mail.MailFormat.Text,String.Empty);
            lblError.Text = "Mail sent successfully";  
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;   
        }
    }
}


Note: If you do not want to attach a file with email, specify blank string as String.Empty. 
 
 
Run the Project.

Download
Download Source Code

No comments:

Post a Comment