结合barcode4j生成条形码 图片以附件的方式发送出去

结合barcode4j生成条形码 并图片以附件的方式发送出去

见以下代码:

Java代码   收藏代码
  1. import java.awt.image.BufferedImage;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.UnsupportedEncodingException;  
  5. import java.util.List;  
  6. import java.util.Properties;  
  7.   
  8. import javax.activation.DataHandler;  
  9. import javax.annotation.Resource;  
  10. import javax.mail.Message;  
  11. import javax.mail.MessagingException;  
  12. import javax.mail.Multipart;  
  13. import javax.mail.Session;  
  14. import javax.mail.Transport;  
  15. import javax.mail.internet.AddressException;  
  16. import javax.mail.internet.InternetAddress;  
  17. import javax.mail.internet.MimeBodyPart;  
  18. import javax.mail.internet.MimeMessage;  
  19. import javax.mail.internet.MimeMultipart;  
  20. import javax.mail.internet.MimeUtility;  
  21. import javax.mail.util.ByteArrayDataSource;  
  22.   
  23. import org.krysalis.barcode4j.impl.code39.Code39Bean;  
  24. import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;  
  25. import org.krysalis.barcode4j.tools.UnitConv;  
  26.   
  27. public void sendEmailModel() {  
  28.         Session m_SmtpSession;  
  29.         String m_SmtpHost = null;  
  30.         String m_SmtpUsername = null;  
  31.         String m_SmtpPassword = null;  
  32.         Properties props = new Properties();  
  33.         // 设置mail服务器  
  34.         props.put("mail.smtp.host", m_SmtpHost);  
  35.         props.put("mail.smtp.auth""true");  
  36.         // Get session  
  37.         m_SmtpSession = Session.getDefaultInstance(props);  
  38.         // watch the mail commands go by to the mail server  
  39.         m_SmtpSession.setDebug(false);  
  40.         try {  
  41.             // Create the barcode bean 条形码生成  
  42.             Code39Bean bean = new Code39Bean();  
  43.             final int dpi = 150;  
  44.             // Configure the barcode generator  
  45.             bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));  
  46.             // makes the narrow bar width exactly one pixel  
  47.             bean.setWideFactor(3);  
  48.             bean.doQuietZone(false);  
  49.             ByteArrayOutputStream out = new ByteArrayOutputStream();  
  50.             BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, "image/jpeg", dpi,  
  51.                     BufferedImage.TYPE_BYTE_BINARY, true0);  
  52.             // Generate the barcode  
  53.             bean.generateBarcode(canvas, "条形源码");  
  54.             // Signal end of generation  
  55.             canvas.finish();  
  56.             // 邮件生成  
  57.             MimeMessage msg = new MimeMessage(m_SmtpSession);  
  58.             // 发送源  
  59.             msg.setFrom(new InternetAddress("xxx@163.com"));  
  60.             // 此处可以发送多个地址  
  61.             InternetAddress[] tos = new InternetAddress[1];  
  62.             tos[0] = new InternetAddress("to@163.com");  
  63.             msg.addRecipients(Message.RecipientType.TO, tos);  
  64.             msg.setSubject("主题""utf-8");  
  65.             // 使用Multipart发送邮件  
  66.             Multipart multipart = new MimeMultipart();  
  67.             // 正文内容  
  68.             MimeBodyPart part = new MimeBodyPart();  
  69.             part.setContent("正文如下:<br/>条形码如下:<br/><img src='cid:barcode.jpg'/>",  
  70.                     "text/html; charset=utf-8");  
  71.             multipart.addBodyPart(part);  
  72.             // 附件  
  73.             part = new MimeBodyPart();  
  74.             ByteArrayDataSource ds = new ByteArrayDataSource(out.toByteArray(),  
  75.                     "application/octet-stream");  
  76.             out.close();// 关闭流  
  77.             // 加入附件  
  78.             part.setDataHandler(new DataHandler(ds));  
  79.             // setHeader 目的是 可以在源码中使用barcode.jpg图片 ,如下: src='cid:barcode.jpg'  
  80.             part.setHeader("Content-ID""barcode.jpg");  
  81.             ds.setName("barcode.jpg");  
  82.             part.setFileName(MimeUtility.encodeText(ds.getName()));  
  83.             multipart.addBodyPart(part);  
  84.             msg.setContent(multipart);  
  85.             msg.saveChanges();  
  86.             Transport transport;  
  87.             // 协议 protocol  
  88.             transport = m_SmtpSession.getTransport("smtp");  
  89.             transport.connect(m_SmtpHost, m_SmtpUsername, m_SmtpPassword);  
  90.             transport.sendMessage(msg, msg.getAllRecipients());  
  91.             transport.close();  
  92.         } catch (IOException e) {  
  93.             e.printStackTrace();  
  94.         } catch (AddressException e) {  
  95.             e.printStackTrace();  
  96.         } catch (MessagingException e) {  
  97.             e.printStackTrace();  
  98.         }  
  99.     }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值