Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)



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

.



Relevant Pages

  • Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
    ... If i want to have different emails set up for various recipients do i ... public static void Send(string templatePath, string templateID, string ... recipients, Hashtable tokens) ... XmlDocument template = new XmlDocument; ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
    ... public static void Send(string templatePath, string templateID, string ... recipients, Hashtable tokens) ... XmlDocument template = new XmlDocument; ... Hashtable tokens) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: automating email with links to records just submitted to an SQL DB using asp.net (vb)
    ... What I usually do for emailing is create an XML file and call it, say, ... public static void Send(string templatePath, string templateID, string ... recipients, Hashtable tokens) ... XmlDocument template = new XmlDocument; ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: using email template asp.net
    ... Okay, well, to send an email, you create a message (string), and send it. ... tokens, add the string as a message to an email, and send it. ... > I want to create a custamizable e-mail template which will contain place ... > to send mails. ...
    (microsoft.public.dotnet.framework.aspnet)
  • User Defined Types
    ... I have a global template in the startup directory. ... the information is combined into one string using the ... I would like to use the Sdr type with the SaveUserInfo code. ...
    (microsoft.public.word.vba.general)