So one nifty thing that I created was a wrapper in C# for creating an email message using a CDONTS object. As far as I understand, the same logic can be used for using different built-in objects on a server.
First I created a message class to encapsulate an email message.
/// <summary>
/// Provides properties and methods for constructing an email message.
/// </summary>
public class MailMessage
{
private string m_To;
private string m_From;
private string m_Subject;
private string m_Body;
private MailFormat m_MailFormat;
public MailMessage()
{
m_MailFormat = MailFormat.Plain;
}
public string To
{
get { return m_To; }
set { m_To = value; }
}
public string From
{
get { return m_From; }
set { m_From = value; }
}
public string Subject
{
get { return m_Subject; }
set { m_Subject = value; }
}
public string Body
{
get
{
switch( MailFormat )
{
case MailFormat.Plain:
return m_Body;
case MailFormat.Html:
StringBuilder sb = new StringBuilder();
sb.Append( @"<!DOCTYPE HTML PUBLIC""-//IETF//DTD HTML 4.0 Transitional//EN"">" );
sb.Append( "<html>" );
sb.Append( "<head>" );
sb.Append( @"<meta http-equiv=""Content-Type""");
sb.Append( @"content=""text/html; charset=iso-8859-1"">" );
sb.Append( @"<meta name=""GENERATOR""" );
sb.Append( @" content=""Jerry's Plumbing Specialties"">" );
sb.Append( String.Format("<title>{0}</title>", m_Subject) );
sb.Append( "</head>" );
sb.Append( @"<body>" );
sb.Append( m_Body );
sb.Append( "</body>" );
sb.Append( "</html>" );
return sb.ToString();
default:
return m_Body;
}
}
set { m_Body = value; }
}
public MailFormat MailFormat
{
get { return m_MailFormat; }
set { m_MailFormat = value; }
}
public bool IsValid
{
get
{
// Return false if any are null
if( (To == null) || (From == null) ||
(Subject == null) || (Body == null) )
return false;
// Return false if any lengths are zero
if( (To.Length == 0) || (From.Length == 0) ||
(Subject.Length == 0) || (Body.Length == 0) )
return false;
return true;
}
}
}
Then here's where the real magic is, the AspEmail class.
public sealed class AspEmail
{
private AspEmail() {}
public static bool Send(MailMessage message)
{
// Make sure the message is valid
if( !message.IsValid )
return false;
// Get the type of the Asp Mail Object
Type AspEmailType = Type.GetTypeFromProgID("CDONTS.NewMail");
// Make sure we could get the type
if( AspEmailType == null )
return false;
// Create an instance of the Type
object aspEmailObject = Activator.CreateInstance(AspEmailType);
// Create and asign our parameter objects
object[] toParam = new object[1];
object[] fromParam = new object[1];
object[] subjectParam = new object[1];
object[] bodyParam = new object[1];
object[] bodyFormatParam = new object[1];
object[] mailFormatParam = new object[1];
toParam[0] = message.To;
fromParam[0] = message.From;
subjectParam[0] = message.Subject;
bodyParam[0] = message.Body;
// Only need these for html?
if( message.MailFormat == MailFormat.Html )
{
bodyFormatParam[0] = 0;
mailFormatParam[0] = 0;
}
try
{
AspEmailType.InvokeMember("To", BindingFlags.SetProperty,
null, aspEmailObject, toParam);
AspEmailType.InvokeMember("From", BindingFlags.SetProperty,
null, aspEmailObject, fromParam);
AspEmailType.InvokeMember("Subject", BindingFlags.SetProperty,
null, aspEmailObject, subjectParam);
AspEmailType.InvokeMember("Body", BindingFlags.SetProperty,
null, aspEmailObject, bodyParam);
if( message.MailFormat == MailFormat.Html )
{
AspEmailType.InvokeMember("BodyFormat", BindingFlags.SetProperty,
null, aspEmailObject, bodyFormatParam);
AspEmailType.InvokeMember("MailFormat", BindingFlags.SetProperty,
null, aspEmailObject, mailFormatParam);
}
AspEmailType.InvokeMember("Send", BindingFlags.InvokeMethod,
null, aspEmailObject, null);
}
catch(Exception exc)
{
throw new Exception("AspEmail Exception!", exc);
}
return true;
}
}
Using reflection we can create the object, set it's properties, and call it's methods. Works great!