Example Of send E-Mail in asp.net with C#


Defaul.aspx

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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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">
    .div
    {
       background-color:Black;
    }
    .onfocuse
    {
      border-color:Red;
      background-color:Yellow;
    }
    .onblur
    {
      border-color:Green;
      background-color:White;
    }
    </style>
    <script  language="javascript">
        function change(obj, event) {

            if (event.type == "focus")
                obj.className = "onfocuse";
            else if (event.type == "blur")
                obj.className = "onblur";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
    <asp:ToolkitScriptManager ID="ScriptManager1" runat="server"></asp:ToolkitScriptManager>
        <asp:Label ID="Label8" ForeColor="Black" Font-Bold="true" Font-Size=Medium BackColor="Yellow" runat="server" Visible="false" Text="Label"></asp:Label>
    <table  class="div" border="1" bordercolor="white">
    <tr>
            <td>
                <asp:Label ID="Label7" ForeColor="White" runat="server" Text="From"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox6" onfocus="change(this,event)" onblur="change(this,event)" runat="server" Height="21px" Width="298px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label1" ForeColor="White" runat="server" Text="TO"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox1" onfocus="change(this,event)" onblur="change(this,event)" runat="server" Height="21px" Width="298px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" ForeColor="White" runat="server" Text="BCC"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox2" onfocus="change(this,event)" onblur="change(this,event)" runat="server" Height="21px" Width="298px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" ForeColor="White" Text="CC"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox3" Height="21px" Width="298px" onfocus="change(this,event)" onblur="change(this,event)" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" ForeColor="White" Text="Subject"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox4" onfocus="change(this,event)" onblur="change(this,event)"  Height="21px" Width="298px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label6" runat="server" ForeColor="White" Text="Attachement"></asp:Label>
            </td>
            <td>
                <%--<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" />--%>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label5" runat="server" ForeColor="White" Text="BOdy"></asp:Label>
            </td>
            <td>
                <asp:HtmlEditorExtender ID="HtmlEditorExtender1" TargetControlID="TextBox5" DisplaySourceTab="true" runat="server">
                </asp:HtmlEditorExtender><br />
                <asp:TextBox ID="TextBox5"  ForeColor="White"  TextMode="MultiLine" runat="server"
                 Width="302px" Height="219px"></asp:TextBox><br /><br />
            </td>
        </tr>
        <tr>
            <td>
               
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Send" onclick="Button1_Click" />
            </td>
        </tr>
    </table></center>
       
    </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.IO;
using System.Web.UI.HtmlControls;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string filea=null;
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress(TextBox6.Text);
        mail.To.Add(TextBox1.Text);
        mail.Bcc.Add(TextBox2.Text);
        mail.CC.Add(TextBox3.Text);
        mail.Subject =TextBox4.Text;
        mail.Body = TextBox5.Text;
        mail.IsBodyHtml = true;
        if(FileUpload1.PostedFile != null)
        {
            HttpPostedFile attfile =FileUpload1.PostedFile;
            int attachrfilelength = attfile.ContentLength;
            if(attachrfilelength>0)
            {
                filea=Path.GetFileName(FileUpload1.PostedFile.FileName);
                FileUpload1.PostedFile.SaveAs(Server.MapPath(filea));
                mail.Attachments.Add(new Attachment(Server.MapPath(filea)));

            }
        }

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("pintu.paghadar@gmail.com", "9429168522");
        SmtpServer.EnableSsl = true;
        SmtpServer.DeliveryMethod=SmtpDeliveryMethod.Network;
        SmtpServer.Send(mail);

       
    }
}

 

Insert Webconfig file this Contain


<mailSettings>
   <smtp from="pintu.paghadar@gmail.com">
    <network host="smtp.gmail.com" password="*******" port="587" userName="pintu.paghadar@gmail.com" />
   </smtp>
  </mailSettings>


0 comments :