Click here to Skip to main content
12,509,728 members (50,581 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

QuestionSending email through hotmail Pin
Member 1263622921-Jul-16 19:59
memberMember 1263622921-Jul-16 19:59 
Questionneed help Pin
Member 1232288511-Feb-16 23:04
memberMember 1232288511-Feb-16 23:04 
AnswerRe: need help Pin
Muhammad Kashif Saeed1-Mar-16 6:15
memberMuhammad Kashif Saeed1-Mar-16 6:15 
QuestionGetting Exception Pin
Member 1198446714-Sep-15 19:30
memberMember 1198446714-Sep-15 19:30 
AnswerRe: Getting Exception Pin
Muhammad Kashif Saeed1-Mar-16 6:16
memberMuhammad Kashif Saeed1-Mar-16 6:16 
Generalthank u sir Pin
mukesh.indya17-Aug-15 17:46
membermukesh.indya17-Aug-15 17:46 
GeneralRe: thank u sir Pin
Muhammad Kashif Saeed1-Mar-16 6:17
memberMuhammad Kashif Saeed1-Mar-16 6:17 
Syed Asim Ashiq8-Aug-15 6:54
memberSyed Asim Ashiq8-Aug-15 6:54 
GeneralThanks! Pin
Member 1186065931-Jul-15 5:12
memberMember 1186065931-Jul-15 5:12 
QuestionWebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for ‘jquery’. Pin
Cooldragonex7-Jul-15 9:24
memberCooldragonex7-Jul-15 9:24 
AnswerRe: WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for ‘jquery’. Pin
Monir Sabbagh20-Feb-16 16:16
memberMonir Sabbagh20-Feb-16 16:16 
QuestionTo Muhammad Kashif saeed Pin
Qaiser10121-Jun-15 4:28
memberQaiser10121-Jun-15 4:28 
AnswerRe: To Muhammad Kashif saeed Pin
Muhammad Kashif Saeed2-Jul-15 2:32
memberMuhammad Kashif Saeed2-Jul-15 2:32 
QuestionProblem in sending mail while the Contact Page is Published in web server. Pin
Member 113496611-Apr-15 9:06
memberMember 113496611-Apr-15 9:06 
AnswerRe: Problem in sending mail while the Contact Page is Published in web server. Pin
Muhammad Kashif Saeed2-Jul-15 2:33
memberMuhammad Kashif Saeed2-Jul-15 2:33 
Questionemail Pin
Member 1147489919-Mar-15 3:07
memberMember 1147489919-Mar-15 3:07 
QuestionEmail Address Pin
Member 114956503-Mar-15 8:01
memberMember 114956503-Mar-15 8:01 
AnswerRe: Email Address Pin
Muhammad Kashif Saeed4-Mar-15 19:27
memberMuhammad Kashif Saeed4-Mar-15 19:27 
Questionstopped working Pin
Member 114035833-Feb-15 0:30
memberMember 114035833-Feb-15 0:30 
QuestionNetworkCredential reference problem help please. Pin
Member 1133603426-Dec-14 0:12
memberMember 1133603426-Dec-14 0:12 
AnswerRe: NetworkCredential reference problem help please. Pin
sjelen3-Feb-15 1:00
membersjelen3-Feb-15 1:00 
QuestionHelp with error. Pin
vcampos200023-Dec-14 14:35
membervcampos200023-Dec-14 14:35 
QuestionSend button not work Pin
Member 1114405210-Oct-14 5:10
memberMember 1114405210-Oct-14 5:10 
QuestionNot Connected Pin
Member 1108172014-Sep-14 8:44
memberMember 1108172014-Sep-14 8:44 
QuestionWhat about the security Pin
Member 1069087819-Jul-14 8:28
memberMember 1069087819-Jul-14 8:28 
Questionsend mail Pin
Member 108697356-Jun-14 20:05
memberMember 108697356-Jun-14 20:05 
AnswerRe: send mail Pin
Muhammad Kashif Saeed7-Jun-14 16:58
memberMuhammad Kashif Saeed7-Jun-14 16:58 
Questionsend button doesnt respond Pin
Member 1084689627-May-14 14:51
memberMember 1084689627-May-14 14:51 
AnswerRe: send button doesnt respond Pin
Muhammad Kashif Saeed7-Jun-14 16:59
memberMuhammad Kashif Saeed7-Jun-14 16:59 
QuestionThanks Pin
SMKarten24-May-14 5:19
memberSMKarten24-May-14 5:19 
Questiona program code for entering employee basic information in asp pages Pin
Member 1074530712-Apr-14 22:24
memberMember 1074530712-Apr-14 22:24 
QuestionSuperb Pin
Santhosh Kumar9-Apr-14 3:50
memberSanthosh Kumar9-Apr-14 3:50 
Questionode is not Pin
ADI@34530-Mar-14 7:17
memberADI@34530-Mar-14 7:17 
AnswerRe: ode is not Pin
Muhammad Kashif Saeed7-Apr-14 19:46
memberMuhammad Kashif Saeed7-Apr-14 19:46 
QuestionThanks Pin
Member 1048684911-Mar-14 22:47
memberMember 1048684911-Mar-14 22:47 
GeneralThanks for detailed article. Pin
Member 1059776914-Feb-14 10:30
memberMember 1059776914-Feb-14 10:30 
GeneralRe: Thanks for detailed article. Pin
Muhammad Kashif Saeed7-Apr-14 19:47
memberMuhammad Kashif Saeed7-Apr-14 19:47 
GeneralEmail Pin
JayJay_3-Feb-14 21:03
memberJayJay_3-Feb-14 21:03 
GeneralRe: Email Pin
Member 1059776914-Feb-14 10:32
memberMember 1059776914-Feb-14 10:32 
GeneralRe: Email Pin
Muhammad Kashif Saeed7-Apr-14 19:52
memberMuhammad Kashif Saeed7-Apr-14 19:52 
QuestionSend Mail / Contact Form using ASP.NET and C# Pin
babajide niffa19-Jan-14 14:42
memberbabajide niffa19-Jan-14 14:42 
QuestionException handlig Pin
BK 4 code25-Dec-13 22:02
professionalBK 4 code25-Dec-13 22:02 
Questionwork local only Pin
elgamal1010-Dec-13 0:41
memberelgamal1010-Dec-13 0:41 
QuestionGetting System.Net.Mail.SmtpException Pin
imastu19-Nov-13 0:19
memberimastu19-Nov-13 0:19 
Questioncan u please send me the database of upload picture Pin
Member 1041034218-Nov-13 17:23
memberMember 1041034218-Nov-13 17:23 
GeneralMy vote of 5 Pin
alaa bektash16-Sep-13 22:13
memberalaa bektash16-Sep-13 22:13 
Questionthrowing error Pin
Andrey Ivanov15-Aug-13 13:01
memberAndrey 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
memberAman Mishra12-Aug-13 21:44 
QuestionGood Example Pin
twjones5912-Jul-13 2:48
membertwjones5912-Jul-13 2:48 
QuestionSMTP Server Error! Pin
Azeem_Khan10-Jul-13 3:28
memberAzeem_Khan10-Jul-13 3: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
Web01 | 2.8.160929.1 | Last Updated 23 Apr 2012
Article Copyright 2012 by Muhammad Kashif Saeed
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid