Showing posts with label asp.net email send. Show all posts
Showing posts with label asp.net email send. Show all posts

Thursday, February 7, 2013

Sending Email from ASP.NET


Send Email From ASP.NET



Create a new page named ContactUs.aspx.


HTML CODE


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <p>
        &nbsp;</p>
    <h3>
        &nbsp;</h3>
    <h3>
        Contact US</h3>
    <p>
        <cc2:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </cc2:ToolkitScriptManager>
    </p>
    <table class="style1">
        <tr>
            <td>
                Name</td>
            <td>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Address</td>
            <td>
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Subject</td>
            <td>
                <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Message</td>
            <td>
                <cc1:Editor ID="EditorEmailMessageBody" runat="server" ActiveMode="Html" 
                    Height="300px" Width="300px" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Button ID="btnSend" runat="server" onclick="btnSend_Click" Text="Send" />
                <asp:Label ID="LabelMessage" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>




In code First include the following 2 namespaces


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



Now write the code for btnSend

 protected void btnSend_Click(object sender, EventArgs e)

    {

        try

        {

            MailMessage mMailMessage = new MailMessage();

            mMailMessage.From = new MailAddress("your gmailaddress");

            mMailMessage.To.Add(txtAddress.Text );


            // mMailMessage.Bcc.Add(new MailAddress(bcc));

            // mMailMessage.CC.Add(new MailAddress(cc));

          

           // Attachment s = new Attachment("c:\\test.txt");

            //  mMailMessage.Attachments.Add(s);


            mMailMessage.Subject = txtSubject .Text;

            mMailMessage.Body = HttpUtility.HtmlEncode(EditorEmailMessageBody.Content);

           // mMailMessage.IsBodyHtml = true;

     //       mMailMessage.Priority = MailPriority.Normal;

            SmtpClient sc = new SmtpClient("smtp.gmail.com");

            sc.Port = 25;


            // sc.Host = "smtp.gmail.com";

            // sc.Port = 587;

            sc.Credentials = new NetworkCredential("gmailid", "password");

            sc.EnableSsl = true;


            //   sc.DeliveryMethod = SmtpDeliveryMethod.Network;


            sc.Send(mMailMessage );

            LabelMessage.Text = "Thank You - Your Message was sent.";

        }

        catch (Exception exp)

        {

            throw new Exception("ERROR: Unable to Send Contact - " + exp.Message.ToString(), exp);

        }

    }


Save and Run.