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
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.
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.
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.
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
Here is a simple way to create an email:
BMail vBMail= new BMail();
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:
Then send the email:
The best way to use this package is to dynamically set the email body and then replace parts of the email dynamically.
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.
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).
- v1.0 - 2005/01/02: First version.
- 2011/08/22: Version compiled with VS2010, FrameWork 4.0. Still working, with some obsolete calls!
I work on a leading European telecom provider regarding on-line real time account management for B2B and B2C customers. Before this position, I worked in one of the leading European council providers of economic forecasts analyses.
I jump into software development in 1985 and never stop! I work with a lot of systems like Apple, NeXT, Unix, Windows. I develop with a lot of languages like Assembler, Pascal, C, C++, Java and C#. I play with databases like Oracle and SQL Server. I love networks and like to make systems working and cooperate themselves.
I'm very interested in MAS: Multi Agent System and really hope that computer will be human in the future. I work on BDI architecture extensions on this purpose, but this is the project of my life!