Introduction
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 a URL, and contains a special dynamic field, a BMAIL tag, that can be replaced at runtime before sending the email. This package will help you in sending 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.
How to dynamically set the email body ?
Note that you can set two kinds of email body: Text and HTML. The email body can be set by different ways. The easiest way is to directly set the body using a string
as a parameter to the setBodyAsText
or setBodyAsHTML
method. But, when the body gets 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 a parameter to setBodyFromFile
. The last way, but not the least, is to use a URL as a 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 a different version of the same email for localization purpose.
What is a dynamic field?
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 BMAIL. Note that the BMAIL tag can also be used inside text only email.
Background
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 automatically send your email, 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.
Installation
Download the Zip file and extract it to your 'c:\temp' folder. The solution has been made with VS.NET 2003 v7.1.3088, with .NET Framework v1.1.4322 SP1.
See also
You can refer to my article "BooProd.Core - Context Sensitive URL" regarding context sensitive URL generation. This package will be useful if you need to replace URLs depending on the execution context.
Using the code
Simple use
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 directly send email or is not trusted by your SMTP relay server, authenticate yourself to your SMTP server in the same way you do 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();
Advanced use
The best way to use this package is to dynamically set the email body and then replace parts of the email dynamically.
vBMail.setBodyFromFile("EMailTemplate.html", true);
vBMail.replaceFrom("name", "John");
vBMail.replaceFrom("number", "1234");
vBMail.replaceFrom("year", DateTime.Now.Year.ToString());
Demo project
A demo application is provided: "BooProd.BMail.Tester". Each step is in a different color.
- Enter "From" and "To" valid email addresses. Check the "notify" box if you want "From" to be notified by email when "To" reads the email. Carbon Copy and Blind Carbon Copy are optional. A default subject is provided. Inside the code, you can provide multiple email addresses for each entry.
- Load the email body. A default body is provided containing three BMail tags. These tags will be replaced dynamically with one of the three methods of the combo box. Inside the code, you can set the email body with your own, using a text file, an HTML file, or a source URL.
- Enter the SMTP server information given by your internet provider. If not provided, you must have a local SMTP server on your host (like the one embedded inside IIS). This host must be granted as your SMTP relay server.
- You can set an attachment file if needed.
- Now you can preview the email and send it.
Enjoy!
Points of interest
- First of all, maybe you don't really understand why you have to use a dedicated email package? My feeling is that when you work within a team, everyone has to use the same development rules. So, here is a way to do this.
- This package can be used as a strong base for any kind of dynamic email generation.
- I dedicate this article to BabyKat and Sirteen who can now generate email without playing PacMan (private joke).
History
- v1.0 - 2005/01/02: First version.
- 2011/08/22: Version compiled with VS2010, FrameWork 4.0. Still working, with some obsolete calls!