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)
{
try
{

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 */


mM.To.Add(txtTo.Text);
/* 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("smtp.mail.yahoo.com");
/* 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.*/


sC.Send(mM);
/* 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" %>
<!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>Sending Mail Using System.Net.Mail</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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>
<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>
</table>

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



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: 127.0.0.1 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:

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.