EDN Admin
Well-known member
<b>Prerequisites</b>
- 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 were 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.
<b>Follow these steps:</b>
- 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.
<img src="http://www.neodynamic.com/Support/FAQs/73/figure01.png
- 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.
<b>Visual Basic .NET</b>
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
<b>Visual C# .NET</b>
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 buttons click event. In the buttons 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.
<b>Visual Basic .NET</b>
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: <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></td></tr><tr><td colspan=""2" <span style=""font-family: Arial Black" ADMIT ONE</td></tr><tr><td bgcolor=""#ff6633"" colspan=""2" <span style=""color: #ffffff; font-family: Arial" NEO STADIUM</td></tr><tr><td colspan=""2" <span style=""font-size: 10pt; font-family: Arial" <b>GENERAL ADMISSION</b></td></tr><tr><td bgcolor=""#ff6633"" colspan=""2" </td></tr><tr><td colspan=""2" <span style=""font-size: 22pt; font-family: Arial" <b>John Doe in Concert</b></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<br />2007</td><td style=""width: 100px" <span style=""font-family: Arial Black" SATURDAY<br />8:00 PM</td></tr><tr><td bgcolor=""#ff6633"" colspan=""2" <span style=""color: #ffffff; font-family: Arial Black" $ 98.00</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
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
<b>Visual C# .NET</b>
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: <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></td></tr><tr><td colspan="2 <span style="font-family: Arial Black ADMIT ONE</td></tr><tr><td bgcolor="#ff6633" colspan="2 <span style="color: #ffffff; font-family: Arial NEO STADIUM</td></tr><tr><td colspan="2 <span style="font-size: 10pt; font-family: Arial <b>GENERAL ADMISSION</b></td></tr><tr><td bgcolor="#ff6633" colspan="2 </td></tr><tr><td colspan="2 <span style="font-size: 22pt; font-family: Arial <b>John Doe in Concert</b></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<br />2007</td><td style="width: 100px <span style="font-family: Arial Black SATURDAY<br />8:00 PM</td></tr><tr><td bgcolor="#ff6633" colspan="2 <span style="color: #ffffff; font-family: Arial Black $ 98.00</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
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);
}
- Thats 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.
<img src="http://www.neodynamic.com/Support/FAQs/73/figure02.png
- After you specify a valid email address and click on "Send Barcode Ticket" a HTML email with barcode image will be send to you.
<img src="http://www.neodynamic.com/Support/FAQs/73/figure03Small.png
<b>Links:</b>
http://www.neodynamic.com/ND/FaqsTipsTricks.aspx?tabid=66&prodid=3&sid=73 This Demo
http://www.neodynamic.com/Products/Demos/Demos.aspx?tabid=74&prodid=3 More Demos
http://www.neodynamic.com/ND/Downloads.aspx?tabid=44&prodid=3 Download Barcode Professional for Windows Forms
http://www.neodynamic.com/Products/BCWinC/BarcodeWinControl.aspx?tabid=23&prodid=3 More Information about Neodynamic Barcode Professional for Windows Forms
Neodynamic
.NET Components & Controls
http://www.neodynamic.com http://www.neodynamic.com
http://www.barcode-for-net.com http://www.barcode-for-net.com
<!--adsense-->
Click here to view the article
- 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 were 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.
<b>Follow these steps:</b>
- 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.
<img src="http://www.neodynamic.com/Support/FAQs/73/figure01.png
- 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.
<b>Visual Basic .NET</b>
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
<b>Visual C# .NET</b>
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 buttons click event. In the buttons 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.
<b>Visual Basic .NET</b>
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: <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></td></tr><tr><td colspan=""2" <span style=""font-family: Arial Black" ADMIT ONE</td></tr><tr><td bgcolor=""#ff6633"" colspan=""2" <span style=""color: #ffffff; font-family: Arial" NEO STADIUM</td></tr><tr><td colspan=""2" <span style=""font-size: 10pt; font-family: Arial" <b>GENERAL ADMISSION</b></td></tr><tr><td bgcolor=""#ff6633"" colspan=""2" </td></tr><tr><td colspan=""2" <span style=""font-size: 22pt; font-family: Arial" <b>John Doe in Concert</b></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<br />2007</td><td style=""width: 100px" <span style=""font-family: Arial Black" SATURDAY<br />8:00 PM</td></tr><tr><td bgcolor=""#ff6633"" colspan=""2" <span style=""color: #ffffff; font-family: Arial Black" $ 98.00</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
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
<b>Visual C# .NET</b>
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: <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></td></tr><tr><td colspan="2 <span style="font-family: Arial Black ADMIT ONE</td></tr><tr><td bgcolor="#ff6633" colspan="2 <span style="color: #ffffff; font-family: Arial NEO STADIUM</td></tr><tr><td colspan="2 <span style="font-size: 10pt; font-family: Arial <b>GENERAL ADMISSION</b></td></tr><tr><td bgcolor="#ff6633" colspan="2 </td></tr><tr><td colspan="2 <span style="font-size: 22pt; font-family: Arial <b>John Doe in Concert</b></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<br />2007</td><td style="width: 100px <span style="font-family: Arial Black SATURDAY<br />8:00 PM</td></tr><tr><td bgcolor="#ff6633" colspan="2 <span style="color: #ffffff; font-family: Arial Black $ 98.00</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
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);
}
- Thats 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.
<img src="http://www.neodynamic.com/Support/FAQs/73/figure02.png
- After you specify a valid email address and click on "Send Barcode Ticket" a HTML email with barcode image will be send to you.
<img src="http://www.neodynamic.com/Support/FAQs/73/figure03Small.png
<b>Links:</b>
http://www.neodynamic.com/ND/FaqsTipsTricks.aspx?tabid=66&prodid=3&sid=73 This Demo
http://www.neodynamic.com/Products/Demos/Demos.aspx?tabid=74&prodid=3 More Demos
http://www.neodynamic.com/ND/Downloads.aspx?tabid=44&prodid=3 Download Barcode Professional for Windows Forms
http://www.neodynamic.com/Products/BCWinC/BarcodeWinControl.aspx?tabid=23&prodid=3 More Information about Neodynamic Barcode Professional for Windows Forms
Neodynamic
.NET Components & Controls
http://www.neodynamic.com http://www.neodynamic.com
http://www.barcode-for-net.com http://www.barcode-for-net.com
<!--adsense-->
Click here to view the article