How to Send Mail form Asp .Net Website

Description

This is a common function Required for every website. For this you have to add 
System.Web.Mail namespace 

The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server. The classes in this namespace can be used from ASP.NET or from any managed application.
For more info about 
System.Web.Mail
click here.

Steps

-Create a website and design the Form like below screen

Image1.jpg

or copy this code in source code and paste within < form > tag

             <table align="center">

                <tr>
                    <td
>

                        <asp:Label ID="Label6" runat="server" Text="From"></asp:Label
>

                    </td
>

                    <td
>

                        <asp:TextBox ID="txtFrom" runat="server" Width="200px"></asp:TextBox
>

                    </td
>

                </tr
>

                <tr
>

                    <td
>

                        <asp:Label ID="Label7" runat="server" Text="Password"></asp:Label
>

                    </td
>

                    <td
>

                        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox
>

                    </td
>

                </tr
>

                <tr
>

                    <td
>

                        <asp:Label ID="Label2" runat="server" Text="To : "></asp:Label
>

                    </td
>

                    <td
>

                        <asp:TextBox ID="txtTo" runat="server" Width="200px"></asp:TextBox
>

                    </td
>

                </tr
>

                <tr
>

                    <td
>

                        <asp:Label ID="Label3" runat="server" Text="Subject :"></asp:Label
>

                    </td
>

                    <td
>

                        <asp:TextBox ID="txtSub" runat="server" Width="200px"></asp:TextBox
>

                    </td
>

                </tr
>

                <tr
>

                    <td
>

                        <asp:Label ID="Label4" runat="server" Text="Message :"></asp:Label
>

                    </td
>

                    <td
>

                        <asp:TextBox ID="txtMsg" runat="server" Height="117px" TextMode="MultiLine" Width="339px"></asp:TextBox
>

                    </td
>

                </tr
>

                <tr
>

                    <td colspan="2" align
="center">

                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text
="Send Mail"

                            OnClientClick="return validate();" Width="123px"
/>

                    </td
>

                </tr
>

                <tr
>

                    <td colspan="2" align
="center">

                        <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label
>

                    </td
>

                </tr
>

            </table
>
 

now go to code view and write below 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.Mail; 

public partial class _Default : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)

    {

        Label1.Text = "";

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        MailMessage mail = new MailMessage();

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

        mail.From = new MailAddress(txtFrom.Text);

        mail.To.Add(txtTo.Text);

        mail.Subject = txtSub.Text;

        mail.Body = txtMsg.Text;

        SmtpServer.Port = 587;

        SmtpServer.Credentials = new System.Net.NetworkCredential(txtFrom.Text, txtPassword.Text);

        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);

        Label1.Text = "Email successfully sent.";
 
    }

}
 

For validation just write the below javascript code in your head tag

           function validate() {

            var msg = document.getElementById('<%=Label1.ClientID %>');
            if (document.getElementById('<%=txtFrom.ClientID %>').value.trim() == "") {

                msg.innerHTML = "Require From mail Id";

                return false;

            }

            else {

                var format = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

                if (format.test(document.getElementById('<%=txtFrom.ClientID %>').value) == false) {

                    msg.innerHTML = "Invalid email";

                    return false;

                }

            }

            if (document.getElementById('<%=txtPassword.ClientID %>').value.trim() == "") {

                msg.innerHTML = "Require password";

                return false;

            }

            if (document.getElementById('<%=txtTo.ClientID %>').value.trim() == "") {

                msg.innerHTML = "Require To mail Id";

                return false;

            }

            else {

                var format = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

                if (format.test(document.getElementById('<%=txtTo.ClientID %>').value) == false) {

                    msg.innerHTML = "Invalid email";

                    return false;

                }

            }

            msg.innerHTML = "";

            return true;

        }

Now you can send mail. 

This example is for sending mail from Gmail. If you want any server mail just change the smtp server details with port number and use it.

Ebook Download
View all
Learn
View all