How to send email using own server smtp setting

June 14, 2013 |

you will be able to send email using asp.net in just few minute

just follow the following step

first of all add reference to  system.web.dll Namspace to your project


Step : 1

 Desing the email.aspx


email.aspx


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

<%@ 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 id="Head1" runat="server">
<title> Send Email</title>

     <style type="text/css">
        #form1
    {
            background-attachment: scroll;
            background-color: #808080;
            border-left-color: inherit;
            border-right-color: inherit;
            border-top-color: inherit;
            border-left: medium none #808080;
            border-right: medium none #808080;
            border-top: thin none #808080;
            border-bottom: thin none #808080;
            background: #CCCCCC;
            height: 1000px;
            color: #808080;
            font-family: "Berlin Sans FB Demi";
            font-size: medium;
            margin-bottom: auto;
            outline-color: #000000;
            }
         .style6
         {
             color: #CC0000;
             font-size: large;
             font-family: Ebrima;
         }
        </style>
</head>
<body style="background:#CCCCC">
    <form id="form1" runat="server">
    <div>
<div>




    <br />
&nbsp;&nbsp;&nbsp;&nbsp;
       
                   
       
       
        style="color: #CC0000; background-color: #CCCCCC; font-size: large; font-weight: 700; font-family: 'Berlin Sans FB Demi';">Home
    &nbsp;<table style="width:100%; height: 413px; margin-top: 0px;">
        <tr>
            <td style="width:20%; text-align:right" class="style6"><strong>To</strong></td>
            <td style="width:80%">
                <asp:TextBox ID="txtTo" runat="server" Width="200px"></asp:TextBox>
            &nbsp;&nbsp;
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                    ControlToValidate="txtTo" ErrorMessage="*" ForeColor="#CC0000"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td style="width:20%; text-align:right" class="style6"><strong>Cc</strong></td>
            <td style="width:80%">
                <asp:TextBox ID="txtCc" runat="server" Width="200px"></asp:TextBox>
            &nbsp;&nbsp;&nbsp;
            </td>
        </tr>
        <tr>
            <td style="width:20%; text-align:right" class="style6"><strong>Bcc</strong></td>
            <td style="width:80%">
                <asp:TextBox ID="txtBcc" runat="server" Width="200px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width:20%; text-align:right" class="style6"><strong>From</strong></td>
            <td style="width:80%">
                <asp:TextBox ID="txtFrom" runat="server" Width="200px"></asp:TextBox>
            &nbsp;&nbsp;
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                    ControlToValidate="txtFrom" ErrorMessage="*" ForeColor="#CC0000"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td style="width:20%; text-align:right" class="style6"><strong>Subject</strong></td>
            <td style="width:80%">
                <asp:TextBox ID="txtSubject" runat="server" Width="200px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width:20%; text-align:right" class="style6"><strong>Body</strong></td>
            <td style="width:80%">
                <asp:TextBox ID="txtBody" runat="server" Height="138px" TextMode="MultiLine"
                    Width="460px"
                    style="font-family: Gautami; font-size: medium; color: #000000"></asp:TextBox>
                <asp:HtmlEditorExtender ID="txtBody_HtmlEditorExtender" runat="server"
                    Enabled="True"  TargetControlID="txtBody" EnableSanitization="False">
                </asp:HtmlEditorExtender>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
                    ControlToValidate="txtBody" ErrorMessage="*" ForeColor="#CC0000"></asp:RequiredFieldValidator>
                <br />
                <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
                </asp:ToolkitScriptManager>
            &nbsp;
            </td>
        </tr>
        <tr>
            <td style="width:20%; text-align:right">&nbsp;</td>
            <td style="width:80%">
                <asp:Button ID="cmdSend" runat="server" onclick="cmdSend_Click" Text="Send"
                    Width="60px" BackColor="White" Font-Italic="False" Font-Size="Large"
                    ForeColor="#CC0000" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="Button1" runat="server" BackColor="White" Font-Bold="False"
                    Font-Size="Medium" ForeColor="#CC0000" onclick="Button1_Click"
                    style="font-size: large" Text="Reset" />
            </td>
        </tr>
        <tr>
            <td style="width:20%; text-align:right">&nbsp;</td>
            <td style="width:80%">
                <asp:Label ID="lblMessage" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
    </div>

    </div>
    </form>
</body>
</html>

Step 2

now code your email.aspx.cs

email.aspx.cs

add namspace using system.net.mail;




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;
using System.Net;
  public partial class email : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cmdSend_Click(object sender, EventArgs e)
        {
            MailMessage mMailMessage = new MailMessage();

            // address of sender
            mMailMessage.From = new MailAddress(txtFrom.Text);
            // recipient address
            mMailMessage.To.Add(new MailAddress(txtTo.Text));
            // Check if the bcc value is empty
            if (txtBcc.Text != string.Empty)
            {
                // Set the Bcc address of the mail message
                mMailMessage.Bcc.Add(new MailAddress(txtBcc.Text));
            }

            // Check if the cc value is empty
            if (txtCc.Text != string.Empty)
            {
                // Set the CC address of the mail message
                mMailMessage.CC.Add(new MailAddress(txtCc.Text));
            }     // Set the subject of the mail message
            mMailMessage.Subject = txtSubject.Text;
            // Set the body of the mail message
            mMailMessage.Body = txtBody.Text;
            // Set the format of the mail message body as HTML
            mMailMessage.IsBodyHtml = true;
            // Set the priority of the mail message to normal
            mMailMessage.Priority = MailPriority.Normal;

            // Instantiate a new instance of SmtpClient
            SmtpClient mSmtpClient = new SmtpClient();
            // Send the mail message
            try
            {
                mSmtpClient.Send(mMailMessage);
            }
            catch (Exception ex)
            {
                ;//log error
                lblMessage.Text = ex.Message;
            }
            finally
            {
                mMailMessage.Dispose();
            }
            ScriptManager.RegisterStartupScript(this, GetType(), "CloseWin", "window.close();", true);
        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            txtTo.Text = string.Empty;
            txtCc.Text = string.Empty;
            txtBcc.Text = string.Empty;
            txtFrom.Text = string.Empty;
            txtSubject.Text = string.Empty;
            txtBody.Text = string.Empty;
        }
    }
step 3
now last and final step

do following in web.config  
 write ur smtp service provider or company server whose email service u are using in place of xyz in following


under the app setting  in web config do following


<appSettings>
    <add key="mail.xyz.com" value="smtp.xyz.com" />
  </appSettings>




and also put this in web config

<system.net>
    <mailSettings>
      <smtp from="support@xyz.com">
        <network host="mail.xyz.com" port="25" userName="support@xyz.com" password="123"/>
      </smtp>
    </mailSettings>
  </system.net>

his is working code guys
any problem then let me know
hope this will be beneficial

add refernce of itext dll file to your project

pdf.aspx

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

<!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 id="Head1" runat="server">
<title>Grdview with Arraylist</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
width:300px;
}
    .style1
    {
        width: 100%;
    }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview"
        AutoGenerateColumns="False" HeaderStyle-BackColor="#7779AF"
        HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField ="Name" HeaderText="Name" />
</Columns>

<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
</asp:GridView>
    <table class="style1">
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:ImageButton ID="btnPdf" runat="server" ImageUrl="~/2.jpg"
onclick="btnExcel_Click" />&nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
<asp:GridView ID="gvdata0" runat="server" CssClass="Gridview" AutoGenerateColumns="False"
                    HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField ="EmpName" HeaderText="EmpName" />
</Columns>

<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
</asp:GridView>
            </td>
        </tr>
    </table>
</div>
</form>
</body>
</html>

pdf.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

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

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        BindGridviewData();
        BindGridviewData1();

    }
    protected void BindGridviewData()
    {
        SqlConnection con = new SqlConnection(Connect.cnst);
        con.Open();
        SqlCommand cmd = new SqlCommand("select [ID],[Name] from exm where ID = '" + TextBox1.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvdata.DataSource = ds;
            gvdata.DataBind();
            int columncount = gvdata.Rows[0].Cells.Count;
            gvdata.Rows[0].Cells.Clear();
            gvdata.Rows[0].Cells.Add(new TableCell());
            gvdata.Rows[0].Cells[0].ColumnSpan = columncount;
            gvdata.Rows[0].Cells[0].Text = "No Records Found";
        }
        else
        {
            gvdata.DataSource = ds;
            gvdata.DataBind();
        }
        con.Close();
    }

    protected void BindGridviewData1()
    {
        SqlConnection con = new SqlConnection(Connect.cnst);
        con.Open();
        SqlCommand cmd = new SqlCommand("select [ID],[EmpName] from tblLogin where ID = '" + TextBox1.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvdata0.DataSource = ds;
            gvdata0.DataBind();
            int columncount = gvdata.Rows[0].Cells.Count;
            gvdata0.Rows[0].Cells.Clear();
            gvdata0.Rows[0].Cells.Add(new TableCell());
            gvdata0.Rows[0].Cells[0].ColumnSpan = columncount;
            gvdata0.Rows[0].Cells[0].Text = "No Records Found";
        }
        else
        {
            gvdata0.DataSource = ds;
            gvdata0.DataBind();
        }
        con.Close();
    }
    protected void btnPdf_Click(object sender, ImageClickEventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //Form.RenderControl(hw);
        gvdata.AllowPaging = false;
        gvdata.DataBind();
        this.BindGridviewData();
        this.BindGridviewData1();
        gvdata.RenderControl(hw);
        gvdata.HeaderRow.Style.Add("width", "15%");
        gvdata.HeaderRow.Style.Add("font-size", "10px");
        gvdata.Style.Add("text-decoration", "none");
        gvdata.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        gvdata.Style.Add("font-size", "8px");
        gvdata0.RenderControl(hw);
        gvdata0.HeaderRow.Style.Add("width", "15%");
        gvdata0.HeaderRow.Style.Add("font-size", "10px");
        gvdata0.Style.Add("text-decoration", "none");
        gvdata0.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        gvdata0.Style.Add("font-size", "8px");
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        //pdfDoc.NewPage();
        pdfDoc.Add(Chunk.NEWLINE);
      
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }
}


output