ANSMTP Developers Center >Programming Serviced Component/COM+ with ANSMTP SMTP Component
Introduction
A serviced component is the mechanism that enables context sharing between COM+ and .NET Framework classes. We can write our serviced component with the powerful COM+ interfaces. In this article, I'll introduce how to use Object Pooling COM+ technology to implement a simple mail list component.
Basic Concept
Object pooling is a COM+ service that enables you to reduce the overhead of creating each object from scratch. When an object is activated, it is pulled from the pool. When the object is deactivated, it is placed back into the pool to await the next request. With this feature, we can write a component which can run in background even the client destroyed object instance.
In the following section, I'll illustrate how to write a serviced component which can send mass emails in background.
Installation and Deployment
You should download the ansmtp installer and install it on your machine at first. If you want to distribute or deploy ansmtp without ansmtp installer, please click here to learn more.
How does it work?
First of all, I designed a xml file named "recipientlist.xml", it represent a mail list with multiple email addresses.
This sample xml includes two mail list, TestGroup has an email address and SupportGroup has two email addresses.
<?xml version="1.0" encoding="utf-8"?>
<recipientlist>
<alias name="TestGroup">
<emailaddress>test@adminsystem.net</emailaddress>
</alias>
<alias name="SupportGroup">
<emailaddress>support@adminsystem.net</emailaddress>
<emailaddress>lvon@adminsystem.net</emailaddress>
</alias>
</recipientlist>
Then I designed a method for this serviced component
public void SendEmail( string senderName,
string senderAddr,
string recipientAlias,
string subject,
string body );
Once client invokes SendEmail method, this serviced component would pick all email addresses from this xml file by specified recipient alias, and send this email to all email addresses in specified mail list.
Why Object Pooling?
If there are many email addresses to send, then it would take a long time to send those emails. Object Pooling enables our serviced component runs in background even the client detroyed this component.
The following code demonstrates how to implement SendEmail method, and how to override CanBePooled method
//============================================================
// SendEmail
//============================================================
public void SendEmail( string senderName,
string senderAddr,
string recipientAlias,
string subject,
string body )
{
CreateObjectInNeed();
m_oSmtp.Reset();
m_oSmtp.From = senderName;
m_oSmtp.FromAddr = senderAddr;
m_oSmtp.Subject = subject;
m_oSmtp.BodyText = body;
m_oSmtp.ServerAddr = ""; //send email via dns lookup.
ArrayList emailAddrs = GetAddressesByAlias( recipientAlias );
int nCount = emailAddrs.Count;
for( int i = 0; i < nCount; i++ )
{
m_oSmtp.ClearRecipient();
m_oSmtp.AddRecipient((string)emailAddrs[i], (string)emailAddrs[i], 0 );
//use MassSender to send email asynchronously
m_oMassSender.Send( m_oSmtp, i, "" );
}
//client invoking finished here, if client destroyed this
//object immediately, then all emails might not be sent by MassSender
// but if we implement Object Pooling in this object,
// this object won't be destroyed.
}
//============================================================
// CanBePooled
//============================================================
protected override bool CanBePooled()
{
client didn't invoke SendEmail, this object needn't be pooled any more
if( m_oMassSender == null )
return false;
if( m_oMassSender.GetQueuedCount() == 0 &&
m_oMassSender.GetIdleThreads() == m_oMassSender.GetCurrentThreads())
{
//all emails were sent, then this object needn't be pooled any more
m_oMassSender.StopAllThreads();
m_oMassSender = null;
m_oSmtp = null;
return false;
}
//this object need to be pooled to keep on sending emails.
return true;
}
How to compile Serviced Component?
First, all assemblies refered in this serviced component need a strong name. Run the following command under DOS prompt can generate .NET Assembly of ANSMTP with a strong name.
rem generate a key file for strong name. sn -k ANSMTPLib.snk rem generate .NET assembly of ANSMTP with strong name tlbimp "C:\Program Files\AdminSystem.NET\ANSMTP\ANSMTP.dll" /keyfile:ANSMTPLib.snk /out:ANSMTPLib.dll /namespace:ANSMTPLib
Then we can compile this sample with the following command
set CompilerPath=%WINDIR%\Microsoft.NET\Framework\v1.0.3705 rem compile emailpool.cs ... %CompilerPath%\csc /t:library /r:System.EnterpriseServices.dll;ANSMTPLib.dll emailpool.cs
We also need to register this dll as COM+ application with regsvcs tool
rem register emailpool.dll as COM+ application regsvcs emailpool.dll
After registered this COM+ application, you can find it in "Control Panel"->"Administrative Tools"->"Component Services"->"COM+ Applications" ->"EmailPoolingComponent". You need to select EmailPoolingComponent->Properties->Advanced, Check "Leave running when idle".
How to use this serviced component?
Add "emailpool.dll" reference to your project, and the following code demonstrates how to invoke this serviced component in C#
using System;
using EmailPoolingComponent;
namespace EmailPoolingComponentClient
{
class Client
{
public static int Main()
{
EmailPooling emailPool = new EmailPooling();
string senderName = "Tester of AdminSystem.NET";
string senderAddr = "test@adminsystem.net";
string subject = "Serviced Component/COM+ Test";
string bodyText = "this is a test";
//SupportGroup is defined in recipientlist.xml
// it refers to a mail list which can include multiple recipients.
string recipientAlias = "SupportGroup";
Console.WriteLine( "Send email to {0}", recipientAlias );
emailPool.SendEmail( senderName,
senderAddr,
recipientAlias,
subject, bodyText);
emailPool.Dispose();
Console.WriteLine( "Email sent" );
return 0;
}
}
}
A Total Sample
Click here to download this sample.
Free Email Support
Not enough? Please contact our technical support team.
Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered
User)
Remarks
We usually reply emails in 24hours. The reason for getting no response is
likely that your SMTP server bounced our reply. In this case, please try to use
another email address to contact us. Your Hotmail or Yahoo email account is
recommended.
|