Tuesday 22 January 2013

How To Send Email Using Asp.net with C#

This is the tutorial for sending the simple email using the asp.net with C#. For this we use this the SMTP of Gmail. Before starting the tutorial you should have a Google Account and if you don't have on you can easily create a free account from here.

Now you have to create a simple web form in Visual Studio as shown in the figure. The code of the following web form is given below.

<%@ 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></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 156px;
        }
        .style3
        {
            width: 156px;
            text-align: right;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style3">
                    Gmail Address</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Width="255px"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"
                        ControlToValidate="TextBox1" ErrorMessage="Valid Email Address..."
                        ForeColor="Red"
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Gmail password</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Width="255px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    To</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Width="255px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Subject</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server" Width="400px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Message</td>
                <td>
                    <asp:TextBox ID="TextBox5" runat="server" Height="74px" TextMode="MultiLine"
                        Width="400px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                        Text="Send Mail" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>



Now your web form is complete and the only task which is remaining is applying the Mail Sending function on the event of Button Click. Before applying the code in the .CS file you have to include following header files on it.

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

Now the code for sending the mail is given as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(TextBox1.Text, "Senders Name");
        msg.To.Add(new MailAddress(TextBox3.Text));
        msg.Subject = TextBox4.Text;
        msg.Body = TextBox5.Text;
        msg.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new NetworkCredential(TextBox1.Text, TextBox2.Text);
        smtp.EnableSsl = true;
        smtp.Send(msg);

    }
}
 
 

Now just compile and run the code. 

No comments:

Post a Comment