有时候我们需要以邮件形式发送附有条形码的门票、实施通讯、请柬。那么面对这样的情况我们该怎么处理呢?今天我们将介绍如何用Barcode Professional生成及发送有条形码的HTML电子邮件。
参考步骤:
- 打开ASP.NET 编辑器(Microsoft Visual Studio 2005, Visual Web Developer Express Edition 或者Microsoft Expression Web)创建ASP.NET网站,添加一个空白页。
- 添加引用 Neodynamic.WebCon 组件。
- 创建一个WebForm,像下图一样。添加一个文本框和一个按钮控制。
- 在WebForm类文件里写入下面的方法。这方法叫GetBarcodeImage,它可生成编码随机值的条码图像,能够嵌入到HTML邮件里。
VB
Private Function GetBarcodeImage As Sy 'Create an instance of BarcodeProfessional class Dim bcp As New Neodynamic.WebCon 'Set barcode settings... 'Code 128 symbology bcp.Symbology = Neodynamic.WebCon 'Set a fictitious value to encode bcp.Code = Guid.NewGuid.ToS("-", "").Substring(0, 20).ToUpper 'Return barcode stream Return New Sy)) End FunctionC#
private Sy GetBarcodeImage { //Create an instance of BarcodeProfessional class Neodynamic.WebCon bcp = new Neodynamic.WebCon; //Set barcode settings... //Code 128 symbology bcp.Symbology = Neodynamic.WebCon; //Set a fictitious value to encode bcp.Code = Guid.NewGuid.ToS("-","").Substring(0,20).ToUpper; //Return barcode stream return new Sy)); }- 在按钮点击事件过程中,用Sy 类嵌入到用上文方法生成的条码图像。
VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As Sy) Handles Bu 'Create the mail message Dim mail As New Sy.MailMessage 'Set the email addresses mail.From = New Sy.MailAddress("me@mycom;) mail.To.Add) '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 = "
" Dim htmlContent2 As String = "" Dim htmlContent3 As String = "NEOMIX | |
ADMIT ONE | |
NEO STADIUM | |
GENERAL ADMISSION | |
John Doe in Concert | |
May 19 2007 | SATURDAY 8:00 PM |
$ 98.00 |
" Dim htmlView As Sy.AlternateView = Sy.AlternateView.CreateAlternateViewFromString(htmlContent1 + htmlContent2 + htmlContent3, Nothing, "text/html") 'Create the LinkedResource (embedded barcode image) Dim barcode As New Sy.LinkedResource, "image/png") barcode.ContentId = "barcodeticket" 'Add the LinkedResource to the view (barcode) 'Add the view mail.Al(htmlView) 'specify the mail server address Dim smtp As New Sy.SmtpClient("127.0.0.1") 'send the message (mail) End Sub
C#
protected void Button1_Click(object sender, EventArgs e) { //Create the mail message Sy.MailMessage mail = new Sy.MailMessage; //Set the email addresses mail.From = new Sy.MailAddress("me@mycom;); mail.To.Add); //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 = "
"; string htmlContent2 = ""; string htmlContent3 = "NEOMIX | |
ADMIT ONE | |
NEO STADIUM | |
GENERAL ADMISSION | |
John Doe in Concert | |
May 19 2007 | SATURDAY 8:00 PM |
$ 98.00 |
"; Sy.AlternateView htmlView = Sy.AlternateView.CreateAlternateViewFromString(htmlContent1 + htmlContent2 + htmlContent3, null, "text/html"); //Create the LinkedResource (embedded barcode image) Sy.LinkedResource barcode = new Sy.LinkedResource, "image/png"); barcode.ContentId = "barcodeticket"; //Add the LinkedResource to the view (barcode); //Add the view mail.Al(htmlView); //specify the mail server address Sy.SmtpClient smtp = new Sy.SmtpClient("127.0.0.1"); //send the message (mail); }
- 完成!建立项目进行测试(上面代码中需指定一个有效的电子邮件账户和SMTP服务器),会得到下图。
当你指定有效地址并点击Send Barcode Ticket后,你将收到上文附有条码的HTML邮件
本文译自neodynamic
本站文章除注明转载外,均为本站原创或翻译