![]() |
General Programming »
Internet / Network »
Email
Intermediate
BooProd.BMail - sending dynamically generated EMailsBy Boo!This package will help you send all kinds of EMails like account creation, order, mailing, reports, ... everything containing dynamic fields. You can configure your own SMTP server, use notification and add attachments. |
C#.NET 1.1, Win2K, WinXPIIS 5.1, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

The purpose of this article is to help you in sending dynamically generated EMails. The EMail body can be set using a text file or an URL, and can contain a special dynamic field, a BMAIL tag, that can be replaced at runtime before sending the EMail. This package will help you send all kinds of EMail like account creation, order, mailing, reports,... everything containing dynamic fields. You can configure your own SMTP server, use notification and add attachments.
Note that you can set two kind of EMail body: Text and HTML. The EMail body can be set by different ways. The easiest way is to set the body directly using a string as parameter to setBodyAsText or setBodyAsHTML method. But, when the body is getting long, it's not very easy to maintain a huge amount of text or HTML inside the code. So, a natural way is to use an external file containing the body of the EMail. You can do this using a path as parameter to setBodyFromFile. The last way, but not the least, is to use an URL as parameter to setBodyFromURL. This is very powerful, as the URL can be a simple HTML file on your server, or even an ASP file. For example, you can use different versions of the same EMail for localization purpose.
A dynamic field is a special tag that will be replaced with an appropriate value just before sending the EMail. So, you can create your EMail template with your favorite HTML editor and insert a special tag, named BMAIL, everywhere you want to use dynamic (runtime depending) values. Then you can use one of the three BMail replacement methods to dynamically replace the HTML code portion of the BMAIL. Note that the BMAIL tag can also be used inside text only EMail.

There's no background needed for using this package but the main problem you can have is with your EMail server. IIS SMTP Virtual server is an Open Relay server and can send your email automatically, but the risk is that the SMTP server of your correspondent will reject the EMail; because your SMTP relay is open or not official (no MX declaration in the DNS zone). So, you can use your own official SMTP account, given by your internet provider.
Download the ZIP file and extract it on your 'c:\temp' folder. The solution has been made with VS.NET 2003 v7.1.3088, with .NET Framework v1.1.4322 SP1.
You can refer to my article "BooProd.Core - Context Sensitive URL" on treating of context sensitive URL generation. This package will be useful if you need to replace URLs depending on the execution context.
Here is a simple way to create an EMail :
BMail vBMail= new BMail();
vBMail.From= "myEMail@xx.com";
vBMail.To= "myFriend@yy.com";
vBMail.Subject= "The subject";
vBMail.setBodyAsText("Hello, this is an EMail");
If your host can't send EMail directly or is not trusted by your SMTP relay server, authenticate yourself to your SMTP server in the same way you did with Outlook.
vBMail.SMTPAutenticate= BMail.SMTP_AUTH_BASIC;
vBMail.SMTPServer= "smtp.myprovider.com";
vBMail.SMTPUserName= "myLogin";
vBMail.SMTPPassword= "myPassword";
Then, send the EMail.
vBMail.send();
The best way to use this package is setting the EMail body dynamically and then replacing parts of the EMail dynamically.
/// The 'true' parameter indicates that the file is an HTML file
vBMail.setBodyFromFile("EMailTemplate.html", true);
/// Replace 3 BMAIL Tag inside the HTML file
vBMail.replaceFrom("name", "John");
vBMail.replaceFrom("number", "1234");
vBMail.replaceFrom("year", DateTime.Now.Year.ToString());
A demo application is provided: "BooProd.BMail.Tester". Each step is in a different color.
Enjoy!
First version.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 2 Jan 2005 Editor: Sumalatha K.R. |
Copyright 2005 by Boo! Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |