zoukankan      html  css  js  c++  java
  • Barcode Professional for ASP.NET使用教程:如何用System.Net.Mail 类创建条码图像的HTML邮件

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

    参考步骤:

    • 打开ASP.NET 编辑器(Microsoft Visual Studio 2005, Visual Web Developer Express Edition 或者Microsoft Expression Web)创建ASP.NET网站,添加一个空白页。
    • 添加引用 Neodynamic.WebControls.BarcodeProfessional.dll 组件。
    • 创建一个WebForm,像下图一样。添加一个文本框和一个按钮控制。

    barcode

    • 在WebForm类文件里写入下面的方法。这方法叫GetBarcodeImage,它可生成编码随机值的条码图像,能够嵌入到HTML邮件里。

    VB

     1 Private Function GetBarcodeImage() As System.IO.MemoryStream
     2  'Create an instance of BarcodeProfessional class
     3  Dim bcp As New Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional()
     4  
     5  'Set barcode settings...
     6  'Code 128 symbology
     7  bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128
     8  'Set a fictitious value to encode
     9  bcp.Code = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 20).ToUpper()
    10  
    11  'Return barcode stream
    12  Return New System.IO.MemoryStream(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png))
    13  End Function

    C#

     1 private System.IO.MemoryStream GetBarcodeImage()
     2  {
     3  //Create an instance of BarcodeProfessional class
     4  Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional();
     5  
     6  //Set barcode settings...
     7  //Code 128 symbology
     8  bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128;
     9  //Set a fictitious value to encode
    10  bcp.Code = Guid.NewGuid().ToString().Replace("-","").Substring(0,20).ToUpper();
    11  
    12  //Return barcode stream
    13  return new System.IO.MemoryStream(bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png));
    14  }
    • 在按钮点击事件过程中,用System.Net.Mail 类嵌入到用上文方法生成的条码图像。

    VB

     1 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     2 'Create the mail message
     3 Dim mail As New System.Net.Mail.MailMessage()
     4 
     5 'Set the email addresses
     6 mail.From = New System.Net.Mail.MailAddress("me@mycompany.com")
     7 mail.To.Add(Me.TextBox1.Text)
     8 
     9 'Set the subject
    10 mail.Subject = "John Doe in Concert - Barcode Ticket"
    11 
    12 'Create the Html part.
    13 'To embed the barcode image, we need to use the prefix cid in the img src attribute.
    14 'The cid value will map to the Content-Id of a Linked resource.
    15 'Example: <img src="cid:barcodeticket"> will map to a LinkedResource with a ContentId of barcodeticket
    16 
    17 Dim htmlContent1 As String = "<table border="" 1""="" bordercolor="" #000000""="" cellpadding="" 5""="" cellspacing="" 0""="" style="" vertical-align:="" middle;="" ="" 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""="">"
    18 Dim htmlContent2 As String = "<img src="cid:barcodeticket">"
    19 Dim htmlContent3 As String = "</td></tr><tr><td style="" ="" 100px""=""><span style="" font-family:="" arial="" black""="">May<br><span style="" font-size:="" 24pt""="">19</span><br>2007</span></td><td style="" ="" 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>"
    20 
    21 Dim htmlView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(htmlContent1 + htmlContent2 + htmlContent3, Nothing, "text/html")
    22 
    23 'Create the LinkedResource (embedded barcode image)
    24 Dim barcode As New System.Net.Mail.LinkedResource(Me.GetBarcodeImage(), "image/png")
    25 barcode.ContentId = "barcodeticket"
    26 'Add the LinkedResource to the view
    27 htmlView.LinkedResources.Add(barcode)
    28 
    29 'Add the view
    30 mail.AlternateViews.Add(htmlView)
    31 
    32 'specify the mail server address
    33 Dim smtp As New System.Net.Mail.SmtpClient("127.0.0.1")
    34 'send the message
    35 smtp.Send(mail)
    36 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;="" ="" 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=""" 100px"=""><span style=""font-family:" arial="" black"="">May<br><span style=""font-size:" 24pt"="">19</span><br>2007</span></td><td style=""" 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);
    }
    • 完成!建立项目进行测试(上面代码中需指定一个有效的电子邮件账户和SMTP服务器),会得到下图。

    barcode

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

    barcode

  • 相关阅读:
    Emacs和ESS的使用技巧。
    响应式
    Day learn,day up
    Docker快速安装kafka | 沈健的技术博客
    闭包函数如何使用循环变量
    leetcode笔记——35.搜索插入位置
    CSS 之动态变换背景颜色
    吴裕雄 PYTHON 神经网络——TENSORFLOW 双隐藏层自编码器设计处理MNIST手写数字数据集并使用TENSORBORD描绘神经网络数据2
    吴裕雄 PYTHON 神经网络——TENSORFLOW 双隐藏层自编码器设计处理MNIST手写数字数据集并使用TENSORBORD描绘神经网络数据
    吴裕雄 PYTHON 神经网络——TENSORFLOW 单隐藏层自编码器设计处理MNIST手写数字数据集并使用TensorBord描绘神经网络数据
  • 原文地址:https://www.cnblogs.com/jp294936239/p/4955431.html
Copyright © 2011-2022 走看看