原創(chuàng)|使用教程|編輯:龔雪|2015-11-10 16:31:03.000|閱讀 386 次
概述:本文將介紹如何用Barcode Professional for ASP.NET來生成及發(fā)送附有條形碼的HTML郵件,附C#和VB代碼
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
有時(shí)候我們需要以郵件形式發(fā)送附有條形碼的門票、實(shí)施通訊、請柬。那么面對這樣的情況我們該怎么處理呢?今天我們將介紹如何用Barcode Professional生成及發(fā)送有條形碼的HTML電子郵件。
參考步驟:
VB
Private Function GetBarcodeImage() As System.IO.MemoryStream 'Create an instance of BarcodeProfessional class Dim bcp As New Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional() 'Set barcode settings... 'Code 128 symbology bcp.Symbology = Neodynamic.WebControls.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
C#
private System.IO.MemoryStream GetBarcodeImage() { //Create an instance of BarcodeProfessional class Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional(); //Set barcode settings... //Code 128 symbology bcp.Symbology = Neodynamic.WebControls.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)); }
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: <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""=""><tbody><tr><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""=""> </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></tbody></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
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: <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\"=""><tbody><tr><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\""> </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></tbody></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); }
當(dāng)你指定有效地址并點(diǎn)擊Send Barcode Ticket后,你將收到上文附有條碼的HTML郵件
本文譯自
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn