Sunday, 13 August 2017

How to send email in ASP.Net using C#

In this post, I will explain How to send email in ASP.Net C#?.

I am going to explain this with below example. In an example, we have one Asp.Net form with few controls as Name, Email, Phone No & Message and Submit Button. On Submit button click application will send one mail to the recipient email. In an example, I am using Gmail SMTP Server and Gmail email address with credentials to send an email. 

Required additional Namespaces - 
using System.Net;
using System.Net.Mail;

Required changes in Gmail account settings to allow sending an email using Asp.Net C# application.
1. Login Gmail account 
2. Go to My Account
3. Go to Sign In & Security
4. Go to Connected apps & sites
5. Change "Allow less secure apps:" settings to "ON"

Below is detailed HTML & C# code to send email using Asp.net C#.

HTML Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendMail.aspx.cs" Inherits="TestApplication.SendMail" %>

<!DOCTYPE html>
<html lang="">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <table cellspacing="0">
                            <tr>
                                <td style="text-align: left; height: 25px;">
                                    <asp:Label ID="lblMessage" runat="server" ForeColor="Red" Text=""></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left">
                                    <asp:Label ID="label10" runat="server" Text="Name :"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left;">
                                    <asp:TextBox ID="txtName" runat="server" ></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left">
                                    <asp:Label ID="label12" runat="server" Text="Email :"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left;">
                                    <asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left">
                                    <asp:Label ID="label1" runat="server" Text="Phone No :"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left;">
                                    <asp:TextBox ID="txtPhoneNo" runat="server" MaxLength="14"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left">
                                    <asp:Label ID="label18" runat="server" Text="Message :"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: left;">
                                    <asp:TextBox ID="txtMessage" runat="server" Height="80px" TextMode="multiline"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: center; padding-top: 10px;">
                                    <asp:Button ID="btnSubmit" runat="server"  OnClick="btnSubmit_Click" Text="SUBMIT" ValidationGroup="Submit" />
                                </td>
                            </tr>
                        </table>
    </div>
    </form>
</body>
</html>


C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

namespace TestApplication
{
    public partial class SendMail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // settings to do with Gmail account
        

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            try
            {
                string Name, Email, PhoneNo,  Message, Body, Subject;

                Name = txtName.Text;
                Email = txtEmail.Text;
                PhoneNo = txtPhoneNo.Text;
                Message = txtMessage.Text;

                Subject = "Message "+ " From " + Name;

                Body = "Name :" + Name + "<br/>" + "<br/>"
                    + "Email :" + Email + "<br/>" + "<br/>"
                    + "PhoneNo :" + PhoneNo + "<br/>" + "<br/>"
                    + "Message :" + Message + "<br/>" + "<br/>"
                     + "Date :" + DateTime.Now.ToString();

                MailMessage Msg = new MailMessage();
                Msg.From = new MailAddress(Email);
                
                //Recipient e-mail address. You can add multiple email address to a recipient list

                Msg.To.Add("abc@gmail.com,info@abc.co.in");

                Msg.Subject = Subject; 
                Msg.Body = Body; 
                Msg.IsBodyHtml = true;

                // SMTP server name or IP and port details.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                //smtp.Host = ""127.0.0.1"";

                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "abc@abc");
                smtp.EnableSsl = true;
                smtp.Send(Msg);

                // Clearvaluess
                txtName.Text = "";
                txtEmail.Text = "";
                txtPhoneNo.Text = "";
                txtMessage.Text = "";

                lblMessage.Text = "Thanks for Contacting us !!!";
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }
        }
    }
}
send email in ASP.Net


0 comments:

Post a Comment

Please do not enter any spam link in the message box.