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

 
AnswerRe: YourName' does not exist PinmemberMuhammad Kashif Saeed15-Jun-13 9:42 
Questionthank you Pinmembervinitha_studies3-Mar-13 4:20 
AnswerRe: thank you PinmemberMuhammad Kashif Saeed15-Jun-13 9:43 
GeneralThnk You so Much :) Pinmemberprashantniku9-Feb-13 0:59 
GeneralRe: Thnk You so Much :) PinmemberMuhammad Kashif Saeed15-Jun-13 9:29 
Questionthanks PinmemberSatheeshK78727-Jan-13 21:37 
AnswerRe: thanks PinmemberMuhammad Kashif Saeed15-Jun-13 9:43 
QuestionThis code is not working Pinmembersabaaqeel26-Dec-12 1:55 
AnswerRe: This code is not working PinmemberMuhammad Kashif Saeed15-Jun-13 9:45 
AnswerThis will help Pinmembersabaaqeel7-Apr-14 4:05 
Generalthanks Pinmemberabhishek kr singh1114-Dec-12 7:42 
GeneralRe: thanks PinmemberMuhammad Kashif Saeed14-Dec-12 23:10 
GeneralMy vote of 5 PinmemberBlackThorn133710-Dec-12 9:17 
QuestionI want mail when i was sent mail PinmemberManoj Maske5-Nov-12 1:10 
When i was send mail then it will go in my send box
but as mention mail id in Email Id Box that mail id will got this mail
so
i want that mail in my inbox
QuestionThe operation has timed out PinmemberSthembiso Mtyongwe23-Oct-12 23:29 
Questionsmtp mail PinmemberJaiswal Ashok18-Sep-12 2:23 
QuestionDefining Proxy in C# plz help .. Pinmemberfizmhd6-Sep-12 18:42 
Questionerror in mail send code Pinmembercsp56715-Aug-12 3:26 
Questionerror in mail send code Pinmembercsp56715-Aug-12 3:26 
QuestionContact Form PinmemberTom Mein2-Aug-12 22:21 
QuestionGetting Error Pinmemberkuldeep158626-Jul-12 2:11 
Questiondo i configure something? PinmemberNuha IT21-Jul-12 7:54 
GeneralThanks PinmemberIBDC20-Jul-12 19:43 
QuestionSends to my email PinmemberEuphorialXTC18-Jul-12 3:15 
QuestionRe: Sends to my email PinmemberHetal Bodar21-Aug-13 18:03 
GeneralMade too simple PinmemberMember 767933026-Jun-12 11:20 
GeneralMy vote of 5 PinmemberMember 791840923-May-12 5:34 
GeneralRe: My vote of 5 PinmemberMuhammad Kashif Saeed23-May-12 8:21 
Generalhelp : Pinmembersimranjeet kaur25-Apr-12 18:51 
GeneralRe: help : Pinmembercboysworld3-May-12 4:19 
GeneralRe: help : PinmemberMuhammad Kashif Saeed3-May-12 7:28 
SuggestionRe: help : PinmemberTee_Love25-Jul-12 2:14 

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 | Mobile
Web01 | 2.8.141022.2 | Last Updated 23 Apr 2012
Article Copyright 2012 by Muhammad Kashif Saeed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid