Posted by : Anwar Hossain
Category : Mail sending in asp.net c#

Send mail with multiple selections in ASP.Net C #.

Dear viewers I will show how to create feedback from with Check box List in ASP.Net C #. Sometimes we need to create mail with multiple selections options in this situation we can use Check box List to choose multiple selected item. In this tutorial I have given complete code for sending mail using Check box List in ASP.Net C Sharp.

Send mail with multiple selections in ASP.Net C #

HTML

<html>

<head id="Head1" runat="server">

    <title></title>

    <style type="text/css">

        .style1

        {

            color: #FF3300;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <fieldset style="width: 40%; background-color: #FFF4DF;">

            <legend>Feedback</legend>

            <table cellpadding="2" cellspacing="5">

                <tr>

                    <td width="80px">

                    </td>

                    <td>

                    </td>

                </tr>

                <tr>

                    <td width="80px">

                        Name<span style="color: #CC3300"> *</span>

                    </td>

                    <td>

                        <asp:TextBox ID="txtName" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Name Required"

                            ForeColor="#FF3300" ControlToValidate="txtName"></asp:RequiredFieldValidator>

                    </td>

                </tr>

                <tr>

                    <td>

                        Subject <span class="style1">*</span>

                    </td>

                    <td>

                        <asp:TextBox ID="txtSubject" runat="server" Width="200px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Subject Required"

                            ForeColor="#FF3300" ControlToValidate="txtSubject"></asp:RequiredFieldValidator>

                    </td>

                </tr>

                <tr>

                    <td>

                        E-mail<span style="color: #CC3300"> *</span>

                    </td>

                    <td>

                        <asp:TextBox ID="txtEmail" runat="server" Width="300px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Email Required"

                            ForeColor="#FF3300" ControlToValidate="txtEmail"></asp:RequiredFieldValidator>

                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Not a vailed email"

                            ControlToValidate="txtEmail" ForeColor="#FF3300" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

                    </td>

                </tr>

                <tr>

                    <td>

                        Inquiry<span style="color: #CC3300"> </span>

                    </td>

                    <td>

                        <asp:TextBox ID="txtInquiry" runat="server" Height="100px" TextMode="MultiLine" Width="400px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>

                        Education

                    </td>

                    <td>

                        <asp:CheckBoxList ID="chkEducation" runat="server" RepeatDirection="Horizontal">

                            <asp:ListItem>O Lavel</asp:ListItem>

                            <asp:ListItem>A Lavel</asp:ListItem>

                            <asp:ListItem>B S C</asp:ListItem>

                            <asp:ListItem>M.sc</asp:ListItem>

                        </asp:CheckBoxList>

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        <asp:Button ID="btnSubmit" runat="server" Text="Send" Width="100px" OnClick="btnSubmit_Click1" />

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                    <td>

                        &nbsp;

                    </td>

                </tr>

            </table>

        </fieldset>

    </div>

    </form>

</body>

</html>

 

CODE BEHIND

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Net.Mail;

using System.Net;

using System.Drawing;

using System.IO;

public partial class FeedbackMultiple_ : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

 

 

    private void EmailFeedback()

    {

 

        //create the mail message

        MailMessage mail = new MailMessage();

 

        //set the addresses

        mail.From = new MailAddress(txtEmail.Text.Trim());

 

        mail.To.Add("anwar@csharpexample.com");

 

        //set the content

        mail.Subject = "Enquiry Form";

 

        //get list item using foreach loop

        string Education = string.Empty;   

        foreach (ListItem item in chkEducation.Items)

        {

            if (item.Selected == true)

            {

                Education += item.Value + "<br>";

            }

        }

 

        //Body to be displayed

 

        mail.Body = "<h2>" + "Enquiry from " + " " + txtName.Text + "</h2>" + "<br><br>" +

                "Subject : " + txtSubject.Text + "<br>" +

                "Email  : " + txtEmail.Text.Trim() + "<br>" +

                "InQuiry : " + txtInquiry.Text.Trim() + "<br>" +

                "Education: " + Education.ToString() + "<br>" +

                "Thank You";

 

 

        mail.IsBodyHtml = true;

 

        // smtp settings

        SmtpClient smtp = new SmtpClient("mail.csharpexample.com");

        NetworkCredential SMTPUserInfo = new NetworkCredential("anwar@csharpexample.com", "Abcd123@fg5");

        smtp.Credentials = SMTPUserInfo;

        smtp.Send(mail);

 

    }

 

    private void Clear()

    {

        txtName.Text = string.Empty;

        txtSubject.Text = string.Empty;

        txtEmail.Text = string.Empty;

        txtInquiry.Text = string.Empty;

        chkEducation.ClearSelection();

 

 

    }

 

    protected void btnSubmit_Click1(object sender, EventArgs e)

    {

 

        EmailFeedback();

        lblMsg.ForeColor = Color.Green;

        Clear();

        lblMsg.Text = "Thank You for Your Inquiry";

 

 

    }

   

}

Out Put

Send-mail-with-multiple-selections



Realted Article Headline

Send mail with multiple selections in ASP.Net C #.
Feedback form with Yes or No Option in ASP.Net C#
Send mail with attachment file in ASP.Net C #
Send mail using Gmail account in ASP.Net C#.
Feedback form example in ASP.Net C#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection