How to create HTML emails featuring barcode images with System.Net.Mail classes in Windows Forms (VB.NET and C#)

- Neodynamic Barcode Professional 3.0 (or greater) for Windows Forms (WinControl)
- Microsoft .NET Framework 2.0 (or greater)
- Microsoft Visual Studio 2005
- Microsoft Visual Studio 2005 Express Editions (VB, C#, J#, and C++)

In the following Step-By-Step Guide we're going to create a simple Windows Form which lets you to generate and send HTML emails featuring barcode images (some useful scenarios may involve any kind of barcode tickets, newsletters, invitations, and so on) created by Barcode Professional for Windows Forms and the System.Net.Mail classes.

Follow these steps:
- Open Visual Studio and create a Windows Forms application project.
- Add a reference to Neodynamic.WinControls.BarcodeProfessional.dll assembly.
- Design the created Windows Form so it looks like the following figure. Add a TextBox and a Button control.

- In the class file of the form write the following private method called GetBarcodeImage() which will generate a barcode image encoding a random value by using Barcode Professional so it can be embedded into the HTML email.

Visual Basic .NET
Private Function GetBarcodeImage() As System.IO.MemoryStream
'Create an instance of BarcodeProfessional class
Dim bcp As New Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional()

'Set barcode settings...
'Code 128 symbology
bcp.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128
'Set a fictitious value to encode
bcp.Code = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 20).ToUpper()

'Return barcode stream
Return New System.IO.MemoryStream(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png))
End Function

Visual C# .NET
private System.IO.MemoryStream GetBarcodeImage()
//Create an instance of BarcodeProfessional class
Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional();

//Set barcode settings...
//Code 128 symbology
bcp.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128;
//Set a fictitious value to encode
bcp.Code = Guid.NewGuid().ToString().Replace("-","").Substring(0,20).ToUpper();

//Return barcode stream
return new System.IO.MemoryStream(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png));

- Now code the button's click event. In the button's click event procedure we are going to generate the HTML email by using System.Net.Mail classes embedding the barcode image generated by the aforementioned private method.

Visual Basic .NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'Create the mail message
Dim mail As New System.Net.Mail.MailMessage()

'Set the email addresses
mail.From = New System.Net.Mail.MailAddress("")

'Set the subject
mail.Subject = "John Doe in Concert - Barcode Ticket"

'Create the Html part.
'To embed the barcode image, we need to use the prefix cid in the img src attribute.
'The cid value will map to the Content-Id of a Linked resource.
'Example: <img src=cid:barcodeticket> will map to a LinkedResource with a ContentId of barcodeticket

Dim htmlContent1 As String = "<table border=""1"" bordercolor=""#000000"" cellpadding=""5"" cellspacing=""0"" style=""vertical-align: middle; width: 300px; text-align: center""><td bgcolor=""#ff6633"" colspan=""2""><span style=""font-size: 10pt; color: #ffffff; font-family: Arial""><b>NEOMIX</b></span></td></tr><tr><td colspan=""2""><span style=""font-family: Arial Black"">ADMIT ONE</span></td></tr><tr><td bgcolor=""#ff6633"" colspan=""2""><span style=""color: #ffffff; font-family: Arial"">NEO STADIUM</span></td></tr><tr><td colspan=""2""><span style=""font-size: 10pt; font-family: Arial""><b>GENERAL ADMISSION</b></span></td></tr><tr><td bgcolor=""#ff6633"" colspan=""2"">&nbsp;</td></tr><tr><td colspan=""2""><span style=""font-size: 22pt; font-family: Arial""><b>John Doe in Concert</b></span></td></tr><tr><td colspan=""2"">"
Dim htmlContent2 As String = "<img src=cid:barcodeticket>"
Dim htmlContent3 As String = "</td></tr><tr><td style=""width: 100px""><span style=""font-family: Arial Black"">May<br /><span style=""font-size: 24pt"">19</span><br />2007</span></td><td style=""width: 100px""><span style=""font-family: Arial Black"">SATURDAY<br />8:00 PM</span></td></tr><tr><td bgcolor=""#ff6633"" colspan=""2""><span style=""color: #ffffff; font-family: Arial Black"">$ 98.00</span></td></tr></table>"

Dim htmlView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(htmlContent1 + htmlContent2 + htmlContent3, Nothing, "text/html")

'Create the LinkedResource (embedded barcode image)
Dim barcode As New System.Net.Mail.LinkedResource(Me.GetBarcodeImage(), "image/png")
barcode.ContentId = "barcodeticket"
'Add the LinkedResource to the view

'Add the view

'specify the mail server address
Dim smtp As New System.Net.Mail.SmtpClient("")
'send the message
End Sub

Visual C# .NET
protected void Button1_Click(object sender, EventArgs e)
//Create the mail message
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

//Set the email addresses
mail.From = new System.Net.Mail.MailAddress("");

//Set the subject
mail.Subject = "John Doe in Concert - Barcode Ticket";

//Create the Html part.
//To embed the barcode image, we need to use the prefix 'cid' in the img src attribute.
//The cid value will map to the Content-Id of a Linked resource.
//Example: <img src=cid:barcodeticket> will map to a LinkedResource with a ContentId of 'barcodeticket'

string htmlContent1 = "<table border=\"1\" bordercolor=\"#000000\" cellpadding=\"5\" cellspacing=\"0\" style=\"vertical-align: middle; width: 300px; text-align: center\"><td bgcolor=\"#ff6633\" colspan=\"2\"><span style=\"font-size: 10pt; color: #ffffff; font-family: Arial\"><b>NEOMIX</b></span></td></tr><tr><td colspan=\"2\"><span style=\"font-family: Arial Black\">ADMIT ONE</span></td></tr><tr><td bgcolor=\"#ff6633\" colspan=\"2\"><span style=\"color: #ffffff; font-family: Arial\">NEO STADIUM</span></td></tr><tr><td colspan=\"2\"><span style=\"font-size: 10pt; font-family: Arial\"><b>GENERAL ADMISSION</b></span></td></tr><tr><td bgcolor=\"#ff6633\" colspan=\"2\">&nbsp;</td></tr><tr><td colspan=\"2\"><span style=\"font-size: 22pt; font-family: Arial\"><b>John Doe in Concert</b></span></td></tr><tr><td colspan=\"2\">";
string htmlContent2 = "<img src=cid:barcodeticket>";
string htmlContent3 = "</td></tr><tr><td style=\"width: 100px\"><span style=\"font-family: Arial Black\">May<br /><span style=\"font-size: 24pt\">19</span><br />2007</span></td><td style=\"width: 100px\"><span style=\"font-family: Arial Black\">SATURDAY<br />8:00 PM</span></td></tr><tr><td bgcolor=\"#ff6633\" colspan=\"2\"><span style=\"color: #ffffff; font-family: Arial Black\">$ 98.00</span></td></tr></table>";

System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(htmlContent1 + htmlContent2 + htmlContent3, null, "text/html");

//Create the LinkedResource (embedded barcode image)
System.Net.Mail.LinkedResource barcode = new System.Net.Mail.LinkedResource(this.GetBarcodeImage(), "image/png");
barcode.ContentId = "barcodeticket";
//Add the LinkedResource to the view

//Add the view

//specify the mail server address
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("");
//send the message

- That's it. Build your project and test it. IMPORTANT: Remember to specify a VALID "FROM" EMAIL ACCOUNT AS WELL AS A VALID SMTP SERVER in the code above.
You should get something like the following figure.

- After you specify a valid email address and click on "Send Barcode Ticket" a HTML email with barcode image will be send to you.

This Demo
More Demos
Download Barcode Professional for Windows Forms
More Information about Neodynamic Barcode Professional for Windows Forms

.NET Components & Controls