This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

15 Nov 2012

Sending Email From your ASP.NET APPLICATION

In this post we will see how to send emails from our ASP.Net application. For this first add a webform to your asp website. Then design the Default.Aspx page as the code given below..

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="height: 654px; width: 101%">
            <tr>
                <td align="center" class="style6">
                    <asp:Label ID="ComposeMail_lbl_display" runat="server" Text="Compose Mail" ForeColor="Red"
                        Font-Size="Large"></asp:Label>
                    <br />
                </td>
            </tr>
            <tr>
                <td align="center" class="style5">
                    <asp:Panel ID="Panel1" runat="server" Width="84%" BorderStyle="Ridge" BackColor="#CCCCCC"
                        BorderColor="#CCCCCC" Height="567px" DefaultButton="btn_SendMail">
                        <table width="80%" style="height: 549px">
                            <tr>
                                <td>
                                    <asp:Label ID="lbl_From" runat="server" Text="From:  " Font-Bold="True"></asp:Label>
                                </td>
                                <td>
                                    <br />
                                    &nbsp;
                                    <asp:TextBox ID="txt_From" runat="server" Width="95%" BorderColor="#FFCC99" BorderStyle="Ridge"
                                        Height="28px"></asp:TextBox>
                                    <br />
                                    <br />
                                </td>
                            </tr>
                            <tr>
                                <td align="left" class="style3" bgcolor="#CCCCCC">
                                    <asp:Label ID="lbl_To" runat="server" Text="To:   " Font-Bold="True"></asp:Label>
                                </td>
                                <td bgcolor="#CCCCCC" class="style4">
                                    &nbsp;
                                    <asp:TextBox ID="txt_To" runat="server" Width="95%" Height="51px" BorderStyle="Ridge"
                                        BorderColor="#FFCC66"></asp:TextBox>
                                    <br />
                                    <br />
                                </td>
                            </tr>
                            <tr>
                                <td align="left" class="style3" bgcolor="#CCCCCC">
                                    <asp:Label ID="lbl_Subject" runat="server" Font-Bold="True" Text="Subject:   "></asp:Label>
                                </td>
                                <td bgcolor="#CCCCCC" class="style4">
                                    &nbsp;
                                    <asp:TextBox ID="txt_Subject" runat="server" Height="34px" Width="95%" BorderStyle="Ridge"
                                        BorderColor="#FFCC66"></asp:TextBox>
                                    <br />
                                    <br />
                                </td>
                            </tr>
                            <tr>
                                <td class="style3" bgcolor="#CCCCCC">
                                </td>
                                <td bgcolor="#CCCCCC" align="center" class="style4">
                                    <asp:TextBox ID="txt_MailBody" runat="server" TextMode="MultiLine" Width="96%" Height="271px"
                                        BorderColor="#FFCC66" BorderStyle="Ridge"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <td>
                                        <asp:Label ID="lbl_msg" runat="server" ForeColor="#FF3300"></asp:Label>
                                        <br />
                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                        <asp:Button ID="btn_SendMail" runat="server" Height="29px" OnClick="ComposeMail_btn_SendMail_Click"
                                            Text="Send Mail" Width="88px" />
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                        <asp:Button ID="btn_clear" runat="server" Height="29px" Text="Clear" 
                                            Width="78px" onclick="btn_clear_Click" />
                                    </td>
                                </td>
                            </tr>
                        </table>
                    </asp:Panel>
                </td>
            </tr>
            <tr>
                <td>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


The c# code in the code behind file is like this..

using System;
using System.Net;

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

    }
    protected void ComposeMail_btn_SendMail_Click(object sender, EventArgs e)
    {
        try
        {
            var fromAddress = txt_From.Text.ToString();
            var toAddress = txt_To.Text.ToString();
            string subject = txt_Subject.Text.ToString();
            string body = "From: " + txt_From.Text + "\n";
            body += "Subject: " + txt_Subject.Text + "\n";
            body += "Mail: " + txt_MailBody.Text + "\n";
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                smtp.Credentials = new NetworkCredential("<e-mail id>", "<password>");
                smtp.Timeout = 20000;
            }
            smtp.Send(fromAddress, toAddress, subject, body);
            lbl_msg.Text = "Congragulations.Mail Sent Successfully...";
        }

        catch (Exception ex)
        {
            lbl_msg.Text = ex.Message.ToString();
        }

    }

    protected void btn_clear_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}




In the above code, you can see, we are using "smtp.gmail.com", means we are sending mail using gmail id. And change the following in the line of code:
smtp.Credentials = new NetworkCredential("<e-mail id>", "<password>");

If u want to use Yahoo mail to send mail, u have to replace "smtp.gmail.com" with "smtp.yahoo.com"

Here ,
<email> -> paste your gmail/yahoo id
<password> -> paste your gmail/yahoo password

Using this, we can send  simple text mail to multiple Email-id's at a time. In the  "To" text box type valid Email-Id's seperated by a comma. The webpage looks like as shown below..

(click on image for larger view)



>>Support blog with your valuable comments..<<

1 comment:

  1. [...] i was working on the Sending mail from asp.net application , i have taken the text box of mail body, with multi line property. But when it is shown in [...]

    ReplyDelete

Thanks for your comments.
-Sameer