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

0 comments:

Post a Comment