Intel® Software Network Knowledge Base Wiki


Constructing Nav Tree
One Moment...

(refresh menu)



 
Welcome, Guest | Quick Login | Register

.NET


Send Email from an ASP .NET Environment

Version 4, Changed by LINDA SWINK on 7/3/2008
Created by: KYLEX.S.LEWIS@INTEL.COM

Challenge

Author: Rahul Guha

Sending email from ASP .NET pages has become a very typical request. In pre-.NET days one had to make use of a COM component (usually CDO or CDONTS), which allowed the developer to send email messages. It required the developer to make sure that the component was installed properly and then maintain the versions of the component.

 

Solution

The smtpMail Object

The .NET framework has inherent support for SMTP, and that makes the life of the developer much easier. The related namespace is called System.Web.Mail. Once included it gives us access to these three classes:

  • MailAttachment. Use this for creating an attachment. To attach a file use the FileName property. This needs to be a physical file path (such as c:/files/xxx.xml), not a relative path (such as files/xxx.xml) or a URL (such as http://myserver/files/xxx.xml).
  • MailMessage. This is the class that builds the message. It has various properties that are mostly mapped to different properties of a mail message, such as:

    Attachments
    Bcc
    Body
    BodyEncoding
    BodyFormat
    Cc
    From
    Headers
    Priority
    Subject
    UrlContentBase
    UrlContentLocation

    Use the three enumerators MailPriority, MailFormat, and MailEncoding for setting proper values in Priority, BodyFormat, and BodyEncoding properties of MailMessage.

    For adding attachments, use the add method in the Attachments collection with the MailAttachment object previously created.
  • smtpMail. This is the class that talks to the CDOSYS objects for Windows* 2000 and sends the mail using SMTP.

    The only property this class has is smtpServer, which is used to find the Windows 2000 server whose SMTP service will be used for sending the email messages. If it is not populated, localhost is used. However, make sure that the SMTP server has permission to relay the email to the exchange server running in the network.

    The only method of this class is send, which is used to actually send the message. It is an overloaded method that sends the message either by passing bare minimum parameters (From, To, Subject, MessageText) or by passing the MailMessage object.

 

Code Listings

MailComponent


            

using System;<br />
using System.Web.Mail ;<br /><br />
namespace MailComponent<br />
{<br />
public class Mail<br />
{<br />
private string m_strTo, m_strCC, m_strFrom, m_strBody;<br /><br />
public Mail( string To, string CC, string From, string Body )<br />
{<br />
//<br />
// TODO: Add constructor logic here<br />
//<br />
strTo = To;<br />
strFrom = From;<br />
strBody = Body;<br />
strCC = CC;<br />
}<br />
public virtual string strTo<br />
{<br />
get<br />
{<br />
return m_strTo;<br />
}<br />
set<br />
{<br />
m_strTo= value;<br />
}<br />
}<br /><br />
public virtual string strCC<br />
{<br />
get<br />
{<br />
return m_strCC;<br />
}<br />
set<br />
{<br />
m_strCC= value;<br />
}<br />
}<br /><br />
public virtual string strFrom<br />
{<br />
get<br />
{<br />
return m_strFrom;<br />
}<br />
set<br />
{<br />
m_strFrom= value;<br />
}<br />
}<br />
public virtual string strBody<br />
{<br />
get<br />
{<br />
return m_strBody;<br />
}<br />
set<br />
{<br />
m_strBody= value;<br />
}<br />
}<br />
<br />
public string SendMail()<br />
{<br />
try<br />
{<br />
<br />
MailMessage msg = new MailMessage();<br />
msg.To = strTo;<br />
msg.Cc = strCC;<br />
msg.From = strFrom;<br />
msg.Body = strBody;<br />
msg.BodyFormat = MailFormat.Html ;<br />
<br />
<br />
MailAttachment at = new MailAttachment(@"c:x.xml");<br />
msg.Attachments.Add (at);<br />
<br />
// The server should have capability to relay the addresses<br />
SmtpMail.SmtpServer = "myserver";<br />
SmtpMail.Send (msg);<br />
return "Success";<br />
}<br />
catch (Exception ex)<br />
{<br />
return ex.ToString();<br />
}<br />
}<br />
<br /><br />
}<br />

 

Calling Function

Now we need to create the code that will consume the Web service from the ASP page. We use an HTTP connection to connect to the Web service with specific query strings, which return the above-mentioned XML strings. We can then use different types of XML parsers to extract data from it.

For the HTTP connection, we use an object called xmlHTTP that is exposed by the Microsoft XML parser that sends the query to the particular Web service method and returns the XML string. So we wrote the following VBScript* file that exposes two methods, which in turn invoke the Web methods of the Web service.

MailConsumer.aspx (portion)

The following code shows how we create an instance of the component that we just created and feed it to send the email. Note that we can send a valid HTML string as the message body; and because we changed the mail format to HTML, it will be rendered properly in an HTLM-aware mail program. In fact, we can have a standard email format stored in the database and manipulate it to send nice looking email messages. (However, that is a different topic and beyond the scope of this discussion.)


            

private void SendEMail()<br />
{<br />
string To = GetTo (System.Int16.Parse(ThreadID));<br />
string CC = GetCC(0);<br />
string Body ;<br />
string url;<br />
Body = "<b>From:</b>" + Request.ServerVariables["LOGON_USER"].ToString() + "<br>";<br />
Body = Body + "<b>To Topic:</b>SomeTopic<br>" ;<br />
Body = Body + "<a href=myurl><b>Reply to the subject</b></a><br><br>";<br />
Body = Body + "<b> Message </b><br>";<br />
Body = Body + "<table border = 1px bordercolor=blue cellpadding=10 cellspacing= 0><tr><td>My Message</td></tr></table>";<br />
<br />
MailComponent.Mail ml = new MailComponent.Mail( To,CC, From, Body );<br />
ml.SendMail();<br />
}

 

The Advantages

This solution offers the following advantages over a normal ASP solution: it is a built-in capability in the .NET framework that makes the developer's job much easier by eliminating the requirement of installation and maintenance of SMTP components in the server.

 

About the Author


Rahul Guha is a senior Software Engineer in the Intel® e-Business Architecture Group, where he is responsible for architecting, designing, and developing several Web-based applications. He has more than eight years of experience in the computing industry, including consulting for different clients from startups to Fortune 500 companies. He joined Intel in February 1999.

 



Served
23 Knowledge Bases
604 Pages
Search
Powering Up Search...


Vote on this Page

Tags For This Page
Loading Tags..

Tag This



Additional legal information