Click here to Skip to main content
13,836,012 members
Click here to Skip to main content
Add your own
alternative version


62 bookmarked
Posted 2 Jan 2005
Licenced CPOL

BooProd.BMail - Sending dynamically generated emails

, 22 Aug 2011
Rate this:
Please Sign up or sign in to vote.
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.


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.


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.

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=    "";
vBMail.To=      "";
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=      "";
vBMail.SMTPUserName=    "myLogin";
vBMail.SMTPPassword=    "myPassword";

Then send the email:


Advanced use

The best way to use this package is to dynamically set the email body and then replace 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());

Demo project

A demo application is provided: "BooProd.BMail.Tester". Each step is in a different color.

  1. 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.
  2. 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.
  3. 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.
  4. You can set an attachment file if needed.
  5. 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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
France France
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!

You may also be interested in...

Comments and Discussions

QuestionAlternative Pin
axuno22-Aug-11 12:26
memberaxuno22-Aug-11 12:26 
QuestionError with VS2010 Pin
Wrangly6-Aug-11 23:21
memberWrangly6-Aug-11 23:21 
AnswerRe: Error with VS2010 Pin
Boo!22-Aug-11 7:31
memberBoo!22-Aug-11 7:31 
GeneralRe: Error with VS2010 Pin
Wrangly22-Aug-11 7:42
memberWrangly22-Aug-11 7:42 
GeneralRe: Error with VS2010 Pin
Boo!22-Aug-11 7:51
memberBoo!22-Aug-11 7:51 
GeneralAxWebBrowser.dll could not found [modified] Pin
MK87_20085-Jan-09 0:02
memberMK87_20085-Jan-09 0:02 
GeneralAxWebBrowser.dll Pin
davidberlin30-May-08 4:25
memberdavidberlin30-May-08 4:25 
GeneralProblem with AxWebBrowser Pin
Aljaz2430-Mar-07 5:18
memberAljaz2430-Mar-07 5:18 
GeneralRe: Problem with AxWebBrowser Pin
Boo!11-Apr-07 13:19
memberBoo!11-Apr-07 13:19 
Generalspam mail Pin
Member 164914317-Jan-07 7:27
memberMember 164914317-Jan-07 7:27 
GeneralRe: spam mail Pin
Boo!17-Jan-07 8:13
memberBoo!17-Jan-07 8:13 
GeneralCDO.Message problem Pin
canhga19-Sep-05 1:18
membercanhga19-Sep-05 1:18 
Questionabout the images??? Pin
Kunal Mukherjee10-Aug-05 10:41
memberKunal Mukherjee10-Aug-05 10:41 
AnswerRe: about the images??? Pin
Boo!15-Aug-05 9:15
memberBoo!15-Aug-05 9:15 
GeneralRe: about the images??? Pin
Kunal Mukherjee15-Aug-05 9:47
memberKunal Mukherjee15-Aug-05 9:47 
GeneralRe: about the images??? Pin
Boo!16-Aug-05 5:11
memberBoo!16-Aug-05 5:11 
GeneralRe: about the images??? Pin
Boo!30-Aug-05 10:43
memberBoo!30-Aug-05 10:43 
QuestionIs it free for use? Pin
Anonymous7-Jun-05 13:56
memberAnonymous7-Jun-05 13:56 
AnswerRe: Is it free for use? Pin
Boo!8-Jun-05 0:15
memberBoo!8-Jun-05 0:15 
GeneralNice Pin
Gary Thom3-Jan-05 7:48
memberGary Thom3-Jan-05 7:48 
GeneralRe: Nice Pin
Boo!3-Jan-05 22:39
memberBoo!3-Jan-05 22:39 
GeneralRe: Nice Pin
Gary Thom4-Jan-05 1:17
memberGary Thom4-Jan-05 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190114.1 | Last Updated 22 Aug 2011
Article Copyright 2005 by Boo!
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid