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