Click here to Skip to main content
12,064,546 members (61,300 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked

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

, 23 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
this is the send mail or contact form in c#


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


This code is tested and running well on gmail account.

Using the code


The code below defines the HTML code of the control:

<%this is the client side code for the design and display%>
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
        Please Fill the Following to Send Mail.</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:" ControlToValidate="YourEmail"
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
            ValidationGroup="save" /><br />
        <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" />
        <asp:Button ID="btnSubmit" runat="server" Text="Send" 
                    OnClick="Button1_Click" ValidationGroup="save" />
    <asp:Label ID="DisplayMessage" runat="server" Visible="false" />

The server side code:

protected void SendMail()
    // Gmail Address from where you send the mail
    var fromAddress = "";
    // 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.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)
        //here on button click what will done 
        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.


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


About the Author

Muhammad Kashif Saeed
Software Developer (Senior) Web Technology Solution
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

Questionthrowing error Pin
Andrey Ivanov15-Aug-13 14:01
memberAndrey Ivanov15-Aug-13 14: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 22:44
memberAman Mishra12-Aug-13 22:44 
QuestionGood Example Pin
twjones5912-Jul-13 3:48
membertwjones5912-Jul-13 3:48 
QuestionSMTP Server Error! Pin
Azeem_Khan10-Jul-13 4:28
memberAzeem_Khan10-Jul-13 4:28 
QuestionSuperb... Pin
boss prabu21-Apr-13 10:05
memberboss prabu21-Apr-13 10:05 
AnswerRe: Superb... Pin
Muhammad Kashif Saeed15-Jun-13 10:31
memberMuhammad Kashif Saeed15-Jun-13 10:31 
Questionerror Pin
Member 995584716-Apr-13 13:11
memberMember 995584716-Apr-13 13:11 
AnswerRe: error Pin
Muhammad Kashif Saeed15-Jun-13 10:33
memberMuhammad Kashif Saeed15-Jun-13 10:33 
Generalcode for reading Pin
Member 997794410-Apr-13 2:33
memberMember 997794410-Apr-13 2:33 
QuestionIts working.................... Pin
SAMNAD.S26-Mar-13 7:18
memberSAMNAD.S26-Mar-13 7:18 
AnswerRe: Its working.................... Pin
Muhammad Kashif Saeed15-Jun-13 10:34
memberMuhammad Kashif Saeed15-Jun-13 10:34 
QuestionA couple of issues Pin
_groo_26-Mar-13 5:42
member_groo_26-Mar-13 5:42 
AnswerRe: A couple of issues Pin
Muhammad Kashif Saeed15-Jun-13 10:37
memberMuhammad Kashif Saeed15-Jun-13 10:37 
QuestionHi Pin
Anil Srivastava8-Mar-13 5:15
memberAnil Srivastava8-Mar-13 5:15 
I download the project.
It is running fine.
It is perfectly running in vs 2010
AnswerRe: Hi Pin
Muhammad Kashif Saeed15-Jun-13 10:38
memberMuhammad Kashif Saeed15-Jun-13 10:38 
Questionnamespace error Pin
jaydip10898-Mar-13 1:18
groupjaydip10898-Mar-13 1:18 
AnswerRe: namespace error Pin
Muhammad Kashif Saeed15-Jun-13 10:39
memberMuhammad Kashif Saeed15-Jun-13 10:39 
QuestionYourName&#39; does not exist Pin
Johann Taljaard3-Mar-13 22:01
memberJohann Taljaard3-Mar-13 22:01 
AnswerRe: YourName' does not exist Pin
Muhammad Kashif Saeed15-Jun-13 10:42
memberMuhammad Kashif Saeed15-Jun-13 10:42 
Questionthank you Pin
vinitha_studies3-Mar-13 5:20
membervinitha_studies3-Mar-13 5:20 
AnswerRe: thank you Pin
Muhammad Kashif Saeed15-Jun-13 10:43
memberMuhammad Kashif Saeed15-Jun-13 10:43 
GeneralThnk You so Much :) Pin
prashantniku9-Feb-13 1:59
memberprashantniku9-Feb-13 1:59 
GeneralRe: Thnk You so Much :) Pin
Muhammad Kashif Saeed15-Jun-13 10:29
memberMuhammad Kashif Saeed15-Jun-13 10:29 
Questionthanks Pin
SatheeshK78727-Jan-13 22:37
memberSatheeshK78727-Jan-13 22:37 
AnswerRe: thanks Pin
Muhammad Kashif Saeed15-Jun-13 10:43
memberMuhammad Kashif Saeed15-Jun-13 10:43 
QuestionThis code is not working Pin
sabaaqeel26-Dec-12 2:55
membersabaaqeel26-Dec-12 2:55 
AnswerRe: This code is not working Pin
Muhammad Kashif Saeed15-Jun-13 10:45
memberMuhammad Kashif Saeed15-Jun-13 10:45 
AnswerThis will help Pin
sabaaqeel7-Apr-14 5:05
membersabaaqeel7-Apr-14 5:05 
Generalthanks Pin
abhishek kr singh1114-Dec-12 8:42
memberabhishek kr singh1114-Dec-12 8:42 
GeneralRe: thanks Pin
Muhammad Kashif Saeed15-Dec-12 0:10
memberMuhammad Kashif Saeed15-Dec-12 0:10 
GeneralMy vote of 5 Pin
BlackThorn133710-Dec-12 10:17
memberBlackThorn133710-Dec-12 10:17 
QuestionI want mail when i was sent mail Pin
Manoj Maske5-Nov-12 2:10
memberManoj Maske5-Nov-12 2:10 
QuestionThe operation has timed out Pin
Sthembiso Mtyongwe24-Oct-12 0:29
memberSthembiso Mtyongwe24-Oct-12 0:29 
AnswerRe: The operation has timed out Pin
vcampos200024-Dec-14 6:36
membervcampos200024-Dec-14 6:36 
Questionsmtp mail Pin
Jaiswal Ashok18-Sep-12 3:23
memberJaiswal Ashok18-Sep-12 3:23 
QuestionDefining Proxy in C# plz help .. Pin
fizmhd6-Sep-12 19:42
memberfizmhd6-Sep-12 19:42 
Questionerror in mail send code Pin
csp56715-Aug-12 4:26
membercsp56715-Aug-12 4:26 
Questionerror in mail send code Pin
csp56715-Aug-12 4:26
membercsp56715-Aug-12 4:26 
QuestionContact Form Pin
Tom Mein2-Aug-12 23:21
memberTom Mein2-Aug-12 23:21 
QuestionGetting Error Pin
kuldeep158626-Jul-12 3:11
memberkuldeep158626-Jul-12 3:11 
Questiondo i configure something? Pin
Nuha IT21-Jul-12 8:54
memberNuha IT21-Jul-12 8:54 
GeneralThanks Pin
IBDC20-Jul-12 20:43
memberIBDC20-Jul-12 20:43 
QuestionSends to my email Pin
EuphorialXTC18-Jul-12 4:15
memberEuphorialXTC18-Jul-12 4:15 
QuestionRe: Sends to my email Pin
Hetal Bodar21-Aug-13 19:03
memberHetal Bodar21-Aug-13 19:03 
GeneralMade too simple Pin
Member 767933026-Jun-12 12:20
memberMember 767933026-Jun-12 12:20 
GeneralMy vote of 5 Pin
Member 791840923-May-12 6:34
memberMember 791840923-May-12 6:34 
GeneralRe: My vote of 5 Pin
Muhammad Kashif Saeed23-May-12 9:21
memberMuhammad Kashif Saeed23-May-12 9:21 
Generalhelp : Pin
simranjeet kaur25-Apr-12 19:51
membersimranjeet kaur25-Apr-12 19:51 
GeneralRe: help : Pin
cboysworld3-May-12 5:19
membercboysworld3-May-12 5:19 
GeneralRe: help : Pin
Muhammad Kashif Saeed3-May-12 8:28
memberMuhammad Kashif Saeed3-May-12 8:28 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.160204.4 | Last Updated 23 Apr 2012
Article Copyright 2012 by Muhammad Kashif Saeed
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid