Sunday, June 24, 2018

HOW TO SEND EMAIL FROM GMAIL SMTP SERVER IN ASP.NET

Introduction:
HERE IN THIS ASP.NET Tutorial  WE WILL USE  TO SEND EMAIL FROM EMAIL SMTP SERVER IN ASP.NET.


Here, we will see how can we send email using our Gmail SMTP in c# .NET. Generally, for sending an email from one place to another two parties are required -- first is the sender and the second is the receiver. We will use the Mail Message class of  .NET to send an email. For this, we will require a few details:
  1. Sender Email Address
  2. Sender Password
  3. Receiver Email Address
  4. Port Number
  5. EnableSSL property
And most important before we start coding we need to import two namespaces to  access the MailMessage Class:
  • using System.Net;
  • using System.Net.Mail;


SMTP Class Properties
Following are the properties of the SMTP class.
Host – SMTP Server URL (Gmail: smtp.gmail.com)
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True)
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password)
Port – Port Number of the SMTP server (Gmail: 587)


Default2.aspx.cs :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 87px;
        }
    </style>
</head>
<body style="height: 126px; width: 708px">
    <form id="form1" runat="server">
    <div>
 
        <table style="width:100%;">
            <tr>
                <td class="style1">
                    <asp:Label ID="Label1" runat="server" Text="FROM :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Width="210px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    <asp:Label ID="Label2" runat="server" Text="TO :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Width="210px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    &nbsp;</td>
                <td>
                    <asp:Label ID="Label3" runat="server" ForeColor="Lime"></asp:Label>
                </td>
                <td>
                    &nbsp;</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.IO;
using System.Net.Mail;
using System.Net;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (MailMessage mm = new MailMessage(TextBox1.Text.ToString(), TextBox2.Text.ToString()))
        {
            mm.Subject = "Admin Notification";
            mm.Body = "Email Send By   " + TextBox1.Text.ToString() + ".";
            mm.IsBodyHtml = true;
            using (SmtpClient smtp = new SmtpClient())
            {
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential("your@gmail.com", "abc");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
                Label3.Text = "Email Send.......";
                 
            }
        }
    }
}

Output :-



























Here Is An Video Related To Topic In This Blog



No comments:

Post a Comment