Barcode Professional for ASP.NET使用教程:如何用System.Net.Mail 类创建条

有时候我们需要以邮件形式发送附有条形码的门票、实施通讯、请柬。那么面对这样的情况我们该怎么处理呢?今天我们将介绍如何用Barcode Professional生成及发送有条形码的HTML电子邮件。

参考步骤:

  • 打开ASP.NET 编辑器(Microsoft Visual Studio 2005, Visual Web Developer Express Edition 或者Microsoft Expression Web)创建ASP.NET网站,添加一个空白页。
  • 添加引用 Neodynamic.WebControls.BarcodeProfessional.dll 组件。
  • 创建一个WebForm,像下图一样。添加一个文本框和一个按钮控制。
  • 在WebForm类文件里写入下面的方法。这方法叫GetBarcodeImage,它可生成编码随机值的条码图像,能够嵌入到HTML邮件里。

VB

PrivateFunctionGetBarcodeImageAsSystem.IO.MemoryStream
'CreateaninstanceofBarcodeProfessionalclass
DimbcpAsNewNeodynamic.WebControls.BarcodeProfessional.BarcodeProfessional

'Setbarcodesettings...
'Code128symbology
bcp.Symbology=Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128
'Setafictitiousvaluetoencode
bcp.Code=Guid.NewGuid.ToString.Replace("-","").Substring(0,20).ToUpper

'Returnbarcodestream
ReturnNewSystem.IO.MemoryStream(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png))
EndFunction

C#

privateSystem.IO.MemoryStreamGetBarcodeImage
{
//CreateaninstanceofBarcodeProfessionalclass
Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessionalbcp=newNeodynamic.WebControls.BarcodeProfessional.BarcodeProfessional;

//Setbarcodesettings...
//Code128symbology
bcp.Symbology=Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128;
//Setafictitiousvaluetoencode
bcp.Code=Guid.NewGuid.ToString.Replace("-","").Substring(0,20).ToUpper;

//Returnbarcodestream
returnnewSystem.IO.MemoryStream(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png));
}
  • 在按钮点击事件过程中,用System.Net.Mail 类嵌入到用上文方法生成的条码图像。

VB

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 = "

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

C#

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 = "

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

"; 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); }

  • 完成!建立项目进行测试(上面代码中需指定一个有效的电子邮件账户和SMTP服务器),会得到下图。
Barcode Professional for ASP.NET使用教程:如何用System.Net.Mail 类创建条码图像的HTML邮件

当你指定有效地址并点击Send Barcode Ticket后,你将收到上文附有条码的HTML邮件

Barcode Professional for ASP.NET使用教程:如何用System.Net.Mail 类创建条码图像的HTML邮件

本文译自neodynamic

本站文章除注明转载外,均为本站原创或翻译

推荐阅读