Click here to Skip to main content
Click here to Skip to main content

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 asp.net c#

Introduction

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

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:

<%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:

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)

Share

About the Author

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

Comments and Discussions

 
QuestionNetworkCredential reference problem help please. PinmemberMember 1133603426-Dec-14 1:12 
QuestionHelp with error. Pinmembervcampos200023-Dec-14 15:35 
QuestionSend button not work PinmemberMember 1114405210-Oct-14 6:10 
QuestionNot Connected PinmemberMember 1108172014-Sep-14 9:44 
QuestionWhat about the security PinmemberMember 1069087819-Jul-14 9:28 
Questionsend mail PinmemberMember 108697356-Jun-14 21:05 
AnswerRe: send mail PinmemberMuhammad Kashif Saeed7-Jun-14 17:58 
Questionsend button doesnt respond PinmemberMember 1084689627-May-14 15:51 
AnswerRe: send button doesnt respond PinmemberMuhammad Kashif Saeed7-Jun-14 17:59 
QuestionThanks PinmemberSMKarten24-May-14 6:19 
Questiona program code for entering employee basic information in asp pages PinmemberMember 1074530712-Apr-14 23:24 
QuestionSuperb PinmemberSanthosh Kumar9-Apr-14 4:50 
Questionode is not PinmemberADI@34530-Mar-14 8:17 
AnswerRe: ode is not PinmemberMuhammad Kashif Saeed7-Apr-14 20:46 
QuestionThanks PinmemberMember 1048684911-Mar-14 23:47 
GeneralThanks for detailed article. PinmemberMember 1059776914-Feb-14 11:30 
GeneralRe: Thanks for detailed article. PinmemberMuhammad Kashif Saeed7-Apr-14 20:47 
GeneralEmail PinmemberJayJay_3-Feb-14 22:03 
GeneralRe: Email PinmemberMember 1059776914-Feb-14 11:32 
GeneralRe: Email PinmemberMuhammad Kashif Saeed7-Apr-14 20:52 
QuestionSend Mail / Contact Form using ASP.NET and C# Pinmemberbabajide niffa19-Jan-14 15:42 
QuestionException handlig PinprofessionalBK 4 code25-Dec-13 23:02 
Questionwork local only Pinmemberelgamal1010-Dec-13 1:41 
QuestionGetting System.Net.Mail.SmtpException Pinmemberimastu19-Nov-13 1:19 
Questioncan u please send me the database of upload picture PinmemberMember 1041034218-Nov-13 18:23 
GeneralMy vote of 5 Pinmemberalaa bektash16-Sep-13 23:13 
Questionthrowing error PinmemberAndrey 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 PinmemberAman Mishra12-Aug-13 22:44 
QuestionGood Example Pinmembertwjones5912-Jul-13 3:48 
QuestionSMTP Server Error! PinmemberAzeem_Khan10-Jul-13 4:28 
QuestionSuperb... Pinmemberboss prabu21-Apr-13 10:05 
AnswerRe: Superb... PinmemberMuhammad Kashif Saeed15-Jun-13 10:31 
Questionerror PinmemberMember 995584716-Apr-13 13:11 
AnswerRe: error PinmemberMuhammad Kashif Saeed15-Jun-13 10:33 
Generalcode for reading PinmemberMember 997794410-Apr-13 2:33 
QuestionIts working.................... PinmemberSAMNAD.S26-Mar-13 7:18 
AnswerRe: Its working.................... PinmemberMuhammad Kashif Saeed15-Jun-13 10:34 
QuestionA couple of issues Pinmember_groo_26-Mar-13 5:42 
AnswerRe: A couple of issues PinmemberMuhammad Kashif Saeed15-Jun-13 10:37 
QuestionHi PinmemberAnil Srivastava8-Mar-13 5:15 
AnswerRe: Hi PinmemberMuhammad Kashif Saeed15-Jun-13 10:38 
Questionnamespace error Pingroupjaydip10898-Mar-13 1:18 
AnswerRe: namespace error PinmemberMuhammad Kashif Saeed15-Jun-13 10:39 
QuestionYourName&#39; does not exist PinmemberJohann Taljaard3-Mar-13 22:01 
AnswerRe: YourName' does not exist PinmemberMuhammad Kashif Saeed15-Jun-13 10:42 
Questionthank you Pinmembervinitha_studies3-Mar-13 5:20 
AnswerRe: thank you PinmemberMuhammad Kashif Saeed15-Jun-13 10:43 
GeneralThnk You so Much :) Pinmemberprashantniku9-Feb-13 1:59 
GeneralRe: Thnk You so Much :) PinmemberMuhammad Kashif Saeed15-Jun-13 10:29 
Questionthanks PinmemberSatheeshK78727-Jan-13 22:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.141223.1 | Last Updated 23 Apr 2012
Article Copyright 2012 by Muhammad Kashif Saeed
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid