Click here to Skip to main content
Click here to Skip to main content

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.

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.

/// 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.

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!

License

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

About the Author

Boo!
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!

Comments and Discussions

 
QuestionAlternative PinmemberNorbert Bietsch22-Aug-11 11:26 
QuestionError with VS2010 PinmemberWrangly6-Aug-11 22:21 
AnswerRe: Error with VS2010 PinmemberBoo!22-Aug-11 6:31 
GeneralRe: Error with VS2010 PinmemberWrangly22-Aug-11 6:42 
GeneralRe: Error with VS2010 PinmemberBoo!22-Aug-11 6:51 
GeneralAxWebBrowser.dll could not found [modified] PinmemberMK87_20084-Jan-09 23:02 
GeneralAxWebBrowser.dll Pinmemberdavidberlin30-May-08 3:25 
GeneralProblem with AxWebBrowser PinmemberAljaz2430-Mar-07 4:18 
GeneralRe: Problem with AxWebBrowser PinmemberBoo!11-Apr-07 12:19 
Generalspam mail PinmemberMember #164914317-Jan-07 6:27 
GeneralRe: spam mail PinmemberBoo!17-Jan-07 7:13 
GeneralCDO.Message problem Pinmembercanhga19-Sep-05 0:18 
Questionabout the images??? PinmemberKunal Mukherjee10-Aug-05 9:41 
AnswerRe: about the images??? PinmemberBoo!15-Aug-05 8:15 
GeneralRe: about the images??? PinmemberKunal Mukherjee15-Aug-05 8:47 
GeneralRe: about the images??? PinmemberBoo!16-Aug-05 4:11 
GeneralRe: about the images??? PinmemberBoo!30-Aug-05 9:43 
QuestionIs it free for use? PinsussAnonymous7-Jun-05 12:56 
AnswerRe: Is it free for use? PinmemberBoo!7-Jun-05 23:15 
GeneralNice PinmemberGary Thom3-Jan-05 6:48 
GeneralRe: Nice PinmemberBoo!3-Jan-05 21:39 
GeneralRe: Nice PinmemberGary Thom4-Jan-05 0:17 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 22 Aug 2011
Article Copyright 2005 by Boo!
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid