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

2024-01-28 11:38

本文主要是介绍结合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.     }  

 

这篇关于结合barcode4j生成条形码 图片以附件的方式发送出去的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/653532

相关文章

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php