Monday, 7 May 2012

How to send a email in c# asp.net using gmail smtp server ?

frmSendEmail.aspx  file


<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/CRP/CRP.Master" AutoEventWireup="true" 
    CodeBehind="frmTest.aspx.cs" Inherits="Ihat_CSMRT.frmTest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server">
  <div>


   <table width="90%">

      <tr><td width="50%" align="right">
       <asp:Label ID="lblEmailAddres" runat="server" Text="To:-"></asp:Label></td>
       <td> <asp:TextBox ID="txtTo" runat="server"></asp:TextBox></td></tr>
       <tr><td width="50%" align="right"><asp:Label ID="lblCcEmailAddress" runat="server"              Text="Cc:-"></asp:Label></td>
   <td><asp:TextBox ID="txtcc" runat="server"></asp:TextBox></td></tr>
   <tr><td width="50%" align="right"><asp:Label ID="lblSubject" runat="server" Text="Subject:-"></asp:Label></td>
       <td><asp:TextBox ID="txtSubject" runat="server"></asp:TextBox></td></tr>
       <tr><td width="50%" align="right"><asp:Label ID="lblContent" runat="server" Text="Message"></asp:Label></td>
       <td><asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine" Height="100px"></asp:TextBox></td></tr>
       <tr><td width="50%" align="right">
       <asp:Button ID="btnSned" runat="server" Text="Send Email" Width="100px" OnClick="btnSned_Click"/></td>
       <td width="50%" align="left"> <asp:Button ID="btnClear" runat="server" Text="Clear" Width="100px" 
           OnClick="btnClear_Click" /></td></tr>
       <tr><td colspan="2"> <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label></td></tr>
        </table>


    </div>

</asp:Content>
    
Design view:-













frmSendEmail.aspx.cs  file

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

namespace Ihat_CSMRT
{
    public partial class frmTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // on page load code here
        }

        protected void btnSned_Click(object sender, EventArgs e)
        {
            if (txtTo.Text != string.Empty && txtcc.Text!=string.Empty && txtSubject.Text != string.Empty && txtContent.Text != string.Empty)
            {
                // call sendEmail method
                sendMail(txtTo.Text, txtcc.Text,txtSubject.Text, txtContent.Text);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtTo.Text = "";
            txtcc.Text = "";
            txtSubject.Text = "";
            txtContent.Text = "";
        }
        public void sendMail(String ClientEmailID, string CcAddress, String subject, String body)
        {
            // create smtp clinet object
            SmtpClient client = new SmtpClient();
            MailMessage message = new MailMessage();
            client.Host = "smtp.gmail.com";
            client.Port = 25;
            MailAddress fromAddress = new MailAddress("rphbti640@gmail.com", "Rampal Singh");
            message.From = fromAddress;
            message.To.Add(ClientEmailID);
            message.Subject = subject;
            message.CC.Add(CcAddress);
            message.Bcc.Add(new MailAddress("rphbti640@gmail.com"));
            message.IsBodyHtml = false;
            message.Body = body;
            client.Credentials = new System.Net.NetworkCredential("rphbti640@gmail.com", "********");
            client.EnableSsl = true;
            try
            {
                client.Send(message);
                lblMsg.Text = "Mail has sent successfully.";

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                lblMsg.Text = "Errors:" + ex;
            }

        }
   
    }

}





No comments:

Post a Comment