Saturday, March 31, 2007

Sending Mail Using System.Net.Mail

Code Behind

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
/* Available Only in .NET 2005, it include:
MailMessage,MailAddress,Attachment,SmtpClient.. */

using System.Drawing;
// to Set ForeColor to Label
using System.IO;

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

MailMessage mM = new MailMessage();
/*Represents an
e-mail message that can be sent using the SmtpClient class.*/

mM.From = new MailAddress(txtFrom.Text);
/* Gets or sets
the from address for this e-mail message */

/* Gets the address collection that
contains the recipients of this e-mail message */

mM.Subject = txtSub.Text;
/* Gets or sets the subject line
for this e-mail message */

mM.Body = txtMsg.Text; /* Message Content */

mM.Priority = MailPriority.High;
/* Gets or sets the priority
of this e-mail message */

SmtpClient sC = new SmtpClient("");
/* Allows applications to
send e-mail by using the Simple Mail Transfer Protocol (SMTP) */

sC.Port = 587;
 /*Gets or sets the port used for SMTP transactions.*/

sC.Credentials = new NetworkCredential("YahooUsername", "password");/
*Provides credentials
for password-based authentication schemes such as basic, digest, NTLM, and
Kerberos authentication.*/

/* Overloaded. Sends an e-mail message to an SMTP server
for delivery. These methods block while the message is being transmitted. */

lbReport.Text = "Mail Send Successfully";

lbReport.ForeColor = Color.Green;

catch(Exception ex)

lbReport.Text = ex+"Mail Sending Fail's";

lbReport.ForeColor = Color.Red;

Inline Code

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>Sending Mail Using System.Net.Mail</title>
<form id="form1" runat="server">
<table style="width: 526px">
<tr><td style="width: 116px; height: 26px;">
<strong>From</strong></td><td style="width: 324px; height: 26px;">
<asp:TextBox ID="txtFrom" runat="server" Width="391px"></asp:TextBox></td>
<tr><td style="width: 116px; height: 26px;">
<strong>To</strong></td><td style="width: 324px; height: 26px;"><asp:TextBox ID="txtTo" runat="server" Width="392px"></asp:TextBox></td></tr>
<tr><td style="width: 116px">
<strong>Subject</strong></td><td style="width: 324px"><asp:TextBox ID="txtSub" runat="server" Width="394px"></asp:TextBox></td> </tr>
<tr><td style="height: 290px">
<strong>Message</strong></td><td style="height: 290px; width: 324px;">
<asp:TextBox ID="txtMsg" runat="server" Height="284px" TextMode="MultiLine" Width="394px"></asp:TextBox></td></tr>
<tr><td style="height: 5px"></td><td style="width: 324px; height: 5px">
<asp:Button ID="btSend" runat="server" Text="Send" OnClick="btSend_Click" /> <asp:Button ID="btClear"
runat="server" Text="Clear" /></td></tr>

<asp:Label ID="lbReport" runat="server" Font-Bold="True" Font-Names="Times New Roman"></asp:Label>

In your IIS Server You Have To Configure,

Step As Follow,

Step 1: Default SMTP Virtual Server.

Step 2:Right-click it.

Step 3:Select Properties.

Step 4:Select Access tab.

Step 5:Click Relay Button.

Step 6:Relay Restriction Dialog Box, only the list below Option Will Be Selected By Default.

Step7:If The local IP address: is Entered Means leave that one else you have to
add the above ip address in The Single Computer Ip address it's enough.

No comments: