Monday 9 April 2012

How to send mail using Asp.net.

To implement the send mail using Asp.net you need to follow the below steps :

Step1 :
Create a new Asp.net website in Visual Studio and write the following html code in the design part of the Default.aspx page. 

Source code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1" align="center" bgcolor="#99CCFF">
            <tr>
                <td class="style5">
                    Your Mail Address<strong>:
                </strong>
                </td>
                <td bgcolor="#99CCFF">
                    <asp:TextBox ID="txtMailAddress" runat="server"></asp:TextBox>
                </td>
            </tr><tr>
                <td class="style2">
                    <span class="style6"><strong>Your Mail Password:</strong></span>
                </td>
                <td bgcolor="#99CCFF">
    <asp:TextBox ID="txtMailPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr><tr>
                <td class="style5">
                    <strong>To</strong></td>
                <td bgcolor="#99CCFF">
    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                </td>
            </tr><tr>
                <td class="style5">
                    <strong>Subject:
                </strong>
                </td>
                <td bgcolor="#99CCFF">
    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                </td>
            </tr><tr>
                <td class="style5">
                    <strong>Attachments :</strong></td>
                <td bgcolor="#99CCFF">
                    <asp:FileUpload ID="fupAttach" runat="server" />
                </td>
            </tr><tr>
                <td class="style5" style="font-weight: 700">
    Message Body:
                </td>
                <td bgcolor="#99CCFF">
                    &nbsp;</td>
            </tr><tr>
                <td class="style2">
                    &nbsp;</td>
                <td bgcolor="#99CCFF">
    <asp:TextBox ID="txtBody" runat="server" Height="113px" TextMode="MultiLine"
      Width="300px"></asp:TextBox>
                </td>
            </tr><tr>
                <td class="style2">
                    &nbsp;</td>
                <td bgcolor="#99CCFF">
    <asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click"
      Text="Send Email" />
                </td>
            </tr><tr>
                <td class="style2">
                    &nbsp;</td>
                <td bgcolor="#99CCFF">
    <asp:Label ID="lblResult" runat="server" style="color: #FF3300"></asp:Label>
                </td></tr>
        </table>
    </div>
    </form>
</body>
</html>



Step2:
Now open the Default.aspx.cs page and write the following source code.

Default.aspx.cs code :
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 Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(txtMailAddress.Text, "Sender's Name");
        msg.To.Add(new MailAddress(txtTo.Text));
        msg.Subject = txtSubject.Text;
        msg.Body = txtBody.Text;
        msg.IsBodyHtml = true;

        if(fupAttach.HasFile)
        {
            msg.Attachments.Add(new Attachment(fupAttach.PostedFile.InputStream, fupAttach.FileName));
        }

        SmtpClient smtp = new SmtpClient();
        smtp.Host="smtp.gmail.com";
        smtp.Credentials= new NetworkCredential(txtMailAddress.Text, txtMailPassword.Text);
        lblResult.Visible=true;
        smtp.EnableSsl=true;
        try
        {
            smtp.Send(msg);
            lblResult.Text="Mail Sent Sucessfully";
        }
        catch(Exception ex)
        {
            lblResult.Text=ex.Message;
        }
    }
}




Step3:
Now build the Solution and Debug it for the output.

OutPut :




No comments:

Post a Comment