5,401,186 members and growing! (17,916 online)
Email Password   helpLost your password?
Web Development » Internet / Network » Email     Intermediate

BooProd.BMail - sending dynamically generated EMails

By 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, WinXP, Windows, .NETIIS, Visual Studio, IIS 5.1, VS.NET2003, Dev

Posted: 2 Jan 2005
Updated: 2 Jan 2005
Views: 34,455
Bookmarked: 30 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 4.48 Rating: 4.30 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 9.1%
3
4 votes, 36.4%
4
6 votes, 54.5%
5

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

How to set the EMail body dynamically?

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.

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

Installation

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.

See also

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.

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 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();

Advanced use

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());

Demo Project

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

  1. Enter "From" and "To" valid EMail address. Check the "notify" box if you want "From" to be notified by EMail when "To" reads the EMail. Carbon Copy and Blind Carbon Copy is 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 on 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 I am using 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Boo!


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

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAxWebBrowser.dllmemberdavidberlin4:25 30 May '08  
GeneralProblem with AxWebBrowsermemberAljaz245:18 30 Mar '07  
GeneralRe: Problem with AxWebBrowsermemberBoo!13:19 11 Apr '07  
Generalspam mailmember7:27 17 Jan '07  
GeneralRe: spam mailmemberBoo!8:13 17 Jan '07  
GeneralCDO.Message problemmembercanhga1:18 19 Sep '05  
Generalabout the images???memberKunal Mukherjee10:41 10 Aug '05  
GeneralRe: about the images???memberBoo!9:15 15 Aug '05  
GeneralRe: about the images???memberKunal Mukherjee9:47 15 Aug '05  
GeneralRe: about the images???memberBoo!5:11 16 Aug '05  
GeneralRe: about the images???memberBoo!10:43 30 Aug '05  
GeneralIs it free for use?sussAnonymous13:56 7 Jun '05  
GeneralRe: Is it free for use?memberBoo!0:15 8 Jun '05  
GeneralNicememberGary Thom7:48 3 Jan '05  
GeneralRe: NicememberBoo!22:39 3 Jan '05  
GeneralRe: NicememberGary Thom1:17 4 Jan '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jan 2005
Editor: Sumalatha K.R.
Copyright 2005 by Boo!
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project