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

Send mail using Gmail account in ASP.Net C#.

Dear viewer’s I will show how to create feedback from in ASP.Net C # for Gmail account. For sending mail using Gmail account we need to ensure to add Gmail host name that means smtp.gmail.com and we need to use port that means port 587 .Beside other necessary namespace need to be imported usual

Send mail using Gmail account 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%;">

            <legend>Feedback</legend>

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

                style="background-color: #808080; color: #FFFFFF" >

               

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

                        &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 Feedbackforgmail : 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@gmail.com");

 

        //set the content

        mail.Subject = "Enquiry Form";

 

        //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>" +             

                "Thank You";

 

        mail.IsBodyHtml = true;

 

        // smtp settings  for Google Account

 

        SmtpClient smtp = new SmtpClient("smtp.gmail.com");


        smtp.Port = 587;


        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;


        NetworkCredential SMTPUserInfo = new NetworkCredential("anwar@gmail.com", "Aic12234@");


        smtp.Timeout = 20000;

        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;

 

    }

     

    protected void btnSubmit_Click1(object sender, EventArgs e)

    {

      

            EmailFeedback();

            lblMsg.ForeColor = Color.Green;

            Clear();

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

 

       

    }

}

 


Out Put

sendmail_using_gmail_account_in_Csharp



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