Wednesday, October 3, 2012

Send Email using gmail account in asp.net and C#

Here i will show how to send Mails using gmail account in asp.net and C#.






Here You need to add two namespaces as shown below
 
using System.Web.UI.WebControls;
using System.Net;
 
ASP.Net :


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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <center>
            <table>
                <tr>
                    <td width="80%">
                        <fieldset>
                            <legend><b>Send E-Mail, using G-Mail Account in ASP.Net</b>
                                </legend>
                            <table width="100%">
                                <tr>
                                    <td colspan="2">
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        From MailID :
                                    </td>
                                    <td align="left">
     <asp:TextBox ID="txtfrom" runat="server" Width="300px"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        Password :
                                    </td>
                                    <td align="left">                                    
     <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" Width="300px"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        To MailID :
                                    </td>
                                    <td align="left">
     <asp:TextBox ID="txtToMailID" runat="server" Width="300px"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        Subject :
                                    </td>
                                    <td align="left">
      <asp:TextBox ID="txtSubject" runat="server" Width="300px"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        Body :
                                    </td>
                                    <td align="left">
      <asp:TextBox ID="txtBody" runat="server" Height="150px" TextMode="MultiLine" Width="300px"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" align="center">
   <asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" />
                                    </td>
                                </tr>
                            </table>
                        </fieldset>
                    </td>
                </tr>
            </table>
        </center>
    </div>
    </form>
</body>
</html>

 Code Behind :



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
 
namespace SendMail
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void btnSend_Click(object sender, EventArgs e)
        {
            SendMail();
        }
 
        private void SendMail()
        {
            try
            {
                var client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential(txtfrom.Text, txtPwd.Text),
                    EnableSsl = true
                };
                client.Send(txtfrom.Text, txtToMailID.Text, txtSubject.Text, txtBody.Text);
            }
            catch (Exception ex)
            {
                string ErrMsg = ex.Message;
            }
        }
     }
}

No comments:

Post a Comment