Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Announcements > Vendor Announcements

Vendor Announcements News and Information from Vendors - New Product Releases, New Versions, ...(moderated)

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-13-2010, 3:07 PM
Neodynamic's Avatar
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Dec 2005
Posts: 94
Reputation: 56
Neodynamic is on a distinguished programming path ahead
Thumbs up How to create HTML emails featuring barcode images with System.Net.Mail classes...

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

Prerequisites
- 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.Barcode Professional()

'Set barcode settings...
'Code 128 symbology
bcp.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbolo gy.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.Barcode Professional bcp = new Neodynamic.WinControls.BarcodeProfessional.Barcode Professional();

//Set barcode settings...
//Code 128 symbology
bcp.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbolo gy.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("me@mycompany.com")
mail.To.Add(Me.TextBox1.Text)

'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: will map to a LinkedResource with a ContentId of barcodeticket

Dim htmlContent1 As String = "
NEOMIX
ADMIT ONE
NEO STADIUM
GENERAL ADMISSION
 
John Doe in Concert
"
Dim htmlContent2 As String = ""
Dim htmlContent3 As String = "
May
19
2007
SATURDAY
8:00 PM
$ 98.00
"

Dim htmlView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewF romString(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
htmlView.LinkedResources.Add(barcode)

'Add the view
mail.AlternateViews.Add(htmlView)

'specify the mail server address
Dim smtp As New System.Net.Mail.SmtpClient("127.0.0.1")
'send the message
smtp.Send(mail)
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("me@mycompany.com");
mail.To.Add(this.TextBox1.Text);

//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: will map to a LinkedResource with a ContentId of 'barcodeticket'

string htmlContent1 = "
NEOMIX
ADMIT ONE
NEO STADIUM
GENERAL ADMISSION
 
John Doe in Concert
";
string htmlContent2 = "";
string htmlContent3 = "
May
19
2007
SATURDAY
8:00 PM
$ 98.00
";

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

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

//Add the view
mail.AlternateViews.Add(htmlView);

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

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



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


Neodynamic
.NET Components & Controls
Neodynamic
ttp://www.barcode-for-net.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
.net, bar code, barcode, neodynamic, windows forms


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 2:11 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.