Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
- From: "Gary" <ggalehouse@xxxxxxxxxx>
- Date: 29 Jun 2006 08:24:43 -0700
Jim,
I was out of the office today, so I just saw your post. I sorta threw
that code out there without much explanation, so don't worry about not
following all of it.
To make a more specific analogy to your application, you could create
an email template like this:
<?xml version="1.0" encoding="utf-8"?>
<email server="smtpserver">
<template Id="OrderConfirmation">
<from><![CDATA[email@xxxxxxxxxx]]></from>
<cc><![CDATA[]]></cc>
<subject><![CDATA[Order confirmation]]></subject>
<message format="HTML"><![CDATA[
<html><body>
Order confirmation for: [~ORDER_ID~]
</body></html>]]>
</message>
</template>
</email>
Create the TemplateEmail class (code below) in a class library below
and add a reference to it in your project.
What you would do to send the email when an order is submitted is
create a function like:
Private Sub SendOrderConfirmation(orderID as string)
Dim properties As Hashtable = new Hashtable()
properties.add("[~ORDER_ID~]", orderID);
//EmailMessage1 one is the ID attribute within the email.config file.
TemplateEmail.Send(path_to_email.config_file, "OrderConfirmation",
properties);
End Sub
So each time an order was submitted you would call
SendOrderConfirmation with the order identifier, and it would
dynamically replace the [~ORDER_ID~] token with the provided orderID.
Let me know if this isn't clear.
Thanks,
Gary
Then I have code like the following to send the actual email:
public class TemplateEmail {
public static void Send(string templatePath, string templateID, string
recipients, Hashtable tokens)
{
//** Load the email configuration template
XmlDocument template = new XmlDocument();
template.Load(templatePath);
//** Pull out the template
XPathNavigator nav = template.DocumentElement.CreateNavigator();
string smtpServer = nav.GetAttribute("SMTPServer", String.Empty);
MailMessage msg = new MailMessage();
//** Process the message elements
XmlNode emailNode =
template.DocumentElement.SelectSingleNode(string.Format("Template[@Id='{0}']",
templateID));
if (emailNode == null) throw new Exception("Invalid or missing email
template identifier");
nav = emailNode.CreateNavigator();
if (!nav.MoveToFirstChild()) throw new Exception("Invalid email
template - no properties defined");
string token = string.Empty;
while (true)
{
switch (nav.Name)
{
case "From":
msg.From = nav.Value;
break;
case "Cc":
msg.Cc = nav.Value;
break;
case "Bcc":
msg.Bcc = nav.Value;
break;
case "Subject":
//** Perform the subject token replacements
msg.Subject = ReplaceTokens(nav.Value, tokens);
break;
case "Body":
//** Perform the subject token replacements
msg.Body = ReplaceTokens(nav.Value, tokens);
break;
default:
break;
}
if (!nav.MoveToNext()) break;
}
//** Send the message
msg.To = recipients;
SmtpMail.SmtpServer = smtpServer;
SmtpMail.Send(msg);
}
private static string ReplaceTokens(string source, Hashtable tokens)
{
string result = source;
foreach (string key in tokens.Keys)
{
result = result.Replace(key, tokens[key].ToString());
}
return result;
}
}
.
- References:
- automating email with links to records just submitted to an SQL DB using asp.net (vb)
- From: Jimbo
- Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
- From: bruce barker \(sqlwork.com\)
- Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
- From: Jimbo
- Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
- From: Gary
- Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
- From: Jimbo
- Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
- From: Jimbo
- automating email with links to records just submitted to an SQL DB using asp.net (vb)
- Prev by Date: Accessing ObjectDataSource within Gridview EditItemTemplate
- Next by Date: Re: FileSize - HowTo
- Previous by thread: Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
- Next by thread: Atlas AutoCompleteExtender not working...
- Index(es):
Relevant Pages
|