Header Ads

SENDING MAIL IN ASP.NET

In IE 5 and 5.5 you could click a button to email a page to a friend. This was pretty cool, and since mail attachments are restricted in many areas due to the proliferation of email-attachment-borne viruses, it was nice that it included the page itself as the body of the message, in HTML format. For some reason, IE 6.0 has lost this functionality, and now will only email pages as attachments (at least that's all my version does). So, for this reason and because it combines a few good concepts, I created an ASP.NET page to allow me to email the contents of a web page to someone. In particular, I use this page to send my HTML formatted newsletter, which uses ASPLists.com's Lyris server, which doesn't allow mail attachments. The code is very simple. The page, with codebehind, is listed below:

EmailWeb.aspx

<%@ Page language="c#" Src="EmailWeb.aspx.cs" Codebehind

="EmailWeb.aspx.cs" AutoEventWireup="false" Inherits="ASPAlliance.Pages.EmailWeb" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table>
<tr>
<td>
URL:
td>
<td>
<asp:TextBox id="txtURL" runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
TO Email:
td>
<td>
<asp:TextBox id="txtTo" runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
FROM Email:
td>
<td>
<asp:TextBox id="txtFrom" runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
Email Subject:
td>
<td>
<asp:TextBox id="txtSubject" runat="server">asp:TextBox>
td>
tr>
table>
<asp:Button id="btnSend" runat="server" Text="Send">asp:Button>
<asp:Label ID="lblResult" Runat="server"/>
<hr>
<asp:Literal ID="litSent" Runat="server"/>
form>
body>
HTML>





using System;

using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Mail;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ASPAlliance.Pages
{
/// &ltsummary>
/// Summary description for EmailWeb.
///
public class EmailWeb : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtURL;
protected System.Web.UI.WebControls.Button btnSend;
protected System.Web.UI.WebControls.TextBox txtTo;
protected System.Web.UI.WebControls.TextBox txtFrom;
protected System.Web.UI.WebControls.TextBox txtSubject;
protected System.Web.UI.WebControls.Label lblResult;
protected System.Web.UI.WebControls.Literal litSent;
protected System.Web.UI.WebControls.TextBox txtTO;

public EmailWeb()
{
Page.Init += new System.EventHandler(Page_Init);
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

private String readHtmlPage(string url)
{
String result;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
private void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
}

#region Web Form Designer generated code
/// &ltsummary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnSend_Click(object sender, System.EventArgs e)
{
String message=readHtmlPage(txtURL.Text);

if (Page.IsValid)
{
Trace.Write("Submit", "Page is valid -- send email.");

try
{
MailMessage Mailer = new MailMessage();
Mailer.From = txtFrom.Text;
Mailer.To = txtTo.Text;
Mailer.Subject = txtSubject.Text;
Mailer.Body = message;
Mailer.BodyFormat = System.Web.Mail.MailFormat.Html;
SmtpMail.Send(Mailer);
lblResult.Text = "Page successfully sent!";
litSent.Text = message;
}
catch(Exception ex)
{
lblResult.Text = "An error occurred: " + ex.ToString();
}
}

}
}
}

No comments:



Powered by Blogger.