Sending Mail Using Asp.Net

Sending Mail Using Asp.Net
How to send email using Gmail SMTP Server in ASP.Net. To send email with Gmail SMTP Server, you will need to use an email address and password of a valid Gmail account and along with that you will need the Gmail SMTP Mail Server settings.



Before sending mail using asp.net,you make sure that you have changed sender mail security settings.For changing gmail setting, please follow the steps which is mentioned below.


First login into Gmail account from which Gmail credentials are you using to send email.

Step1: After sign in to your account,please select menu on right most top,then click on My Account.























Step2: Select "Connected apps & sites" which is under sign in & security.




Step3: Finally, you have to Turn ON,Allow less secure apps.

Turn off(default)




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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="subject"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label2" runat="server" Text="Message"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox><br />
        <asp:Label ID="Label3" runat="server" Text="To"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="send" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

c# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

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

        protected void Button1_Click(object sender, EventArgs e)
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com");

            var Message = new MailMessage();

            client.Host = "smtp.gmail.com";
            client.Port = 587;

            client.UseDefaultCredentials = false;

            client.Credentials = new System.Net.NetworkCredential("SENDERMAIL ID", "SENDERPASSWORD");

            client.EnableSsl = true;

            MailAddress SendFrom = new MailAddress("SENDERMAILID", "");
 try
            {
 MailAddress SendTo = new MailAddress(TextBox3.Text);

                Message = new MailMessage(SendFrom, SendTo);

                Message.Subject = TextBox1.Text;

                Message.Body = TextBox2.Text;
                Message.IsBodyHtml = true;

                Message.Priority = MailPriority.High;

                client.ServicePoint.MaxIdleTime = 0;

                client.ServicePoint.ConnectionLimit = 1;

                client.Send(Message);

                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Mail Send successfully');</script>");
             
            }

            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Unexpected Error Occur TryAgain ');</script>");
             
            }
        }
    }
}

output:

Figure(1):After typing all content,Click "Send Button".



Figure(2):You can see Notification at the top.




Figure(3)Finally,open you mail id,you have receive message.




Thanks for visiting...


Share this

Related Posts

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
:-?
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
$-)
(y)
(f)
x-)
(k)
(h)
cheer