Click here to Skip to main content
15,881,380 members
Articles / Web Development / ASP.NET
Tip/Trick

Send Mail / Contact Form using ASP.NET and C#

Rate me:
Please Sign up or sign in to vote.
4.80/5 (53 votes)
23 Apr 2012CPOL 2.3M   40.5K   34   103
this is the send mail or contact form in asp.net c#

Introduction

For sending mail through ASP.NET and C# website:

Image 1

Background

This code is tested and running well on gmail account.

Using the code

HTML

The code below defines the HTML code of the control:

XML
<%this is the client side code for the design and display%>
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
    <p>
        Please Fill the Following to Send Mail.</p>
    <p>
        Your name:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
            ControlToValidate="YourName" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
        Your email address:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
            ControlToValidate="YourEmail" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
        <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
            SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
            ValidationGroup="save" /><br />
        Subject:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
            ControlToValidate="YourSubject" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
        Your Question:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
            ControlToValidate="Comments" ValidationGroup="save" /><br />
        <asp:TextBox ID="Comments" runat="server" 
                TextMode="MultiLine" Rows="10" Width="400px" />
    </p>
    <p>
        <asp:Button ID="btnSubmit" runat="server" Text="Send" 
                    OnClick="Button1_Click" ValidationGroup="save" />
    </p>
</asp:Panel>
<p>
    <asp:Label ID="DisplayMessage" runat="server" Visible="false" />
</p>  

The server side code:

C#
protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "Gmail@gmail.com";
    // any address where the email will be sending
    var toAddress = YourEmail.Text.ToString(); 
    //Password of your gmail address
    const string fromPassword = "Password";
     // Passing the values and make a email formate to display
    string subject = YourSubject.Text.ToString();
    string body = "From: " + YourName.Text + "\n";
    body += "Email: " + YourEmail.Text + "\n";
    body += "Subject: " + YourSubject.Text + "\n";
    body += "Question: \n" + Comments.Text + "\n";
    // smtp settings
    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(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
}

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        //here on button click what will done 
        SendMail();
        DisplayMessage.Text = "Your Comments after sending the mail";
        DisplayMessage.Visible = true;
        YourSubject.Text = "";
        YourEmail.Text = "";
        YourName.Text = "";
        Comments.Text = "";
    }
    catch (Exception) { }
}

The above code has the validation checks also.

This code is based on the 3.5 framework and also works in the 4.0 framework.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Web Technology Solution
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncan u please send me the database of upload picture Pin
Member 1041034218-Nov-13 17:23
Member 1041034218-Nov-13 17:23 
GeneralMy vote of 5 Pin
alaa bektash16-Sep-13 22:13
alaa bektash16-Sep-13 22:13 
Questionthrowing error Pin
Andrey Ivanov15-Aug-13 13:01
Andrey Ivanov15-Aug-13 13:01 
Questioni have create this code in contact us page..........please tell me send button working completely(send mail) in c# code Pin
Aman Mishra12-Aug-13 21:44
Aman Mishra12-Aug-13 21:44 
QuestionGood Example Pin
Cybervol12-Jul-13 2:48
Cybervol12-Jul-13 2:48 
QuestionSMTP Server Error! Pin
Azeem_Khan10-Jul-13 3:28
Azeem_Khan10-Jul-13 3:28 
QuestionSuperb... Pin
boss prabu21-Apr-13 9:05
boss prabu21-Apr-13 9:05 
AnswerRe: Superb... Pin
Muhammad Kashif Saeed15-Jun-13 9:31
Muhammad Kashif Saeed15-Jun-13 9:31 
Questionerror Pin
Member 995584716-Apr-13 12:11
Member 995584716-Apr-13 12:11 
AnswerRe: error Pin
Muhammad Kashif Saeed15-Jun-13 9:33
Muhammad Kashif Saeed15-Jun-13 9:33 
Generalcode for reading Pin
Hannan Munshi10-Apr-13 1:33
professionalHannan Munshi10-Apr-13 1:33 
QuestionIts working.................... Pin
SAMNAD.S26-Mar-13 6:18
SAMNAD.S26-Mar-13 6:18 
AnswerRe: Its working.................... Pin
Muhammad Kashif Saeed15-Jun-13 9:34
Muhammad Kashif Saeed15-Jun-13 9:34 
QuestionA couple of issues Pin
_groo_26-Mar-13 4:42
_groo_26-Mar-13 4:42 
AnswerRe: A couple of issues Pin
Muhammad Kashif Saeed15-Jun-13 9:37
Muhammad Kashif Saeed15-Jun-13 9:37 
QuestionHi Pin
Anil Srivastava8-Mar-13 4:15
Anil Srivastava8-Mar-13 4:15 
AnswerRe: Hi Pin
Muhammad Kashif Saeed15-Jun-13 9:38
Muhammad Kashif Saeed15-Jun-13 9:38 
Questionnamespace error Pin
jaydip10898-Mar-13 0:18
jaydip10898-Mar-13 0:18 
AnswerRe: namespace error Pin
Muhammad Kashif Saeed15-Jun-13 9:39
Muhammad Kashif Saeed15-Jun-13 9:39 
QuestionYourName&#39; does not exist Pin
Johann Taljaard3-Mar-13 21:01
Johann Taljaard3-Mar-13 21:01 
AnswerRe: YourName' does not exist Pin
Muhammad Kashif Saeed15-Jun-13 9:42
Muhammad Kashif Saeed15-Jun-13 9:42 
Questionthank you Pin
vinitha_studies3-Mar-13 4:20
vinitha_studies3-Mar-13 4:20 
AnswerRe: thank you Pin
Muhammad Kashif Saeed15-Jun-13 9:43
Muhammad Kashif Saeed15-Jun-13 9:43 
GeneralThnk You so Much :) Pin
prashantniku9-Feb-13 0:59
prashantniku9-Feb-13 0:59 
GeneralRe: Thnk You so Much :) Pin
Muhammad Kashif Saeed15-Jun-13 9:29
Muhammad Kashif Saeed15-Jun-13 9:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.