当前位置: 首页 > news >正文

生成不保存在服务器的附件,并以附件形式发送邮件

需求:从数据库中抓取需要的数据,将数据生成excel表格,并将此表格以附件的形式放置到邮件中发送

    //发送带附件的邮件,同时附件不会生成到服务器中public static String sendFileEmail(String form, String code, String to, String title, String context, Map<String,String> custBO,List<Map<String,String>> MSG) throws AddressException, MessagingException {Properties properties = new Properties();properties.put("mail.transport.protocol", "smtp");// 连接协议properties.put("mail.smtp.host", "smtp.exmail.qq.com");// 主机名properties.put("mail.smtp.port", 465);// 端口号properties.put("mail.smtp.auth", "true");properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息// 得到回话对象Session session = Session.getInstance(properties);// 获取邮件对象Message message = new MimeMessage(session);// 设置发件人邮箱地址message.setFrom(new InternetAddress(form));// 设置收件人邮箱地址message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) });// message.setRecipient(Message.RecipientType.TO, new// InternetAddress("xxx@qq.com"));//一个收件人// 设置邮件标题message.setSubject(title);// 设置附件// 整封邮件的MINE消息体MimeMultipart msgMultipart = new MimeMultipart("mixed");// 混合的组合关系// 设置邮件的MINE消息体message.setContent(msgMultipart);BodyPart bp = new MimeBodyPart();bp.setContent(context, "text/html;charset=gbk");msgMultipart.addBodyPart(bp);
//==================================================================================================MimeBodyPart attch = new MimeBodyPart(); // 附件msgMultipart.addBodyPart(attch); // 将附件添加到MIME消息体中ByteArrayDataSource dataSource = null; // 数据源ByteArrayOutputStream os = new ByteArrayOutputStream();HSSFWorkbook wb = new HSSFWorkbook();String deliveryNo = custBO.get("deliveryNo");// 发货订单号HSSFSheet sheet = wb.createSheet("sheet页的标题" + deliveryNo);// 设置列名HSSFRow titlerRow = sheet.createRow(0);titlerRow.createCell(0).setCellValue("新编码");titlerRow.createCell(1).setCellValue("老编码");titlerRow.createCell(2).setCellValue("数量");// 添加子表得内容for (Map<String,String> bo : MSG) {// 获取最后一行的行号int lastRowNum = sheet.getLastRowNum();HSSFRow dataRow = sheet.createRow(lastRowNum + 1);dataRow.createCell(0).setCellValue(bo.get("新编码"));dataRow.createCell(1).setCellValue(bo.get("老编码"));dataRow.createCell(2).setCellValue(bo.get("数量"));}try {wb.write(os);} catch (IOException e) {e.printStackTrace();}ByteArrayInputStream a = new ByteArrayInputStream(os.toByteArray());
//====================================================================================================Transport transport = null;try {dataSource = new ByteArrayDataSource(a, "xlsx/data");attch.setDataHandler(new DataHandler(dataSource));attch.setFileName("文件名" + ".xlsx");// 得到邮差对象transport = session.getTransport();// 连接自己的邮箱账户transport.connect(form, code);// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
//     // 设置邮件内容
//        message.setText(context);// 发送邮件transport.sendMessage(message, message.getAllRecipients());} catch (IOException e) {e.printStackTrace();} finally {transport.close();}return null;}

http://www.lryc.cn/news/58104.html

相关文章:

  • Golang Gin框架HTTP上传文件
  • BM36-判断是不是平衡二叉树
  • Quartz 单例定时任务
  • 不要告诉同事你要离职!打算跳槽,新公司开出两倍薪资,私下告诉要好的同事,却被同事出卖给领导!...
  • RK3399平台开发系列讲解(外设篇)Camera OV13850配置过程
  • yolov8训练自己的数据集
  • 【产品经理】对接第三方平台,你应该怎么做?
  • Hbase 介绍
  • 金三银四没把握住,凉了...
  • 模拟axios请求的数据Mockjs在vue3的使用
  • Elasticsearch:索引状态是红色还是黄色?为什么?
  • 一对多关系映射
  • 字母有重复全排列 [2*]
  • 机器学习中的数学原理——过拟合、正则化与惩罚函数
  • RK3588S imx415摄像头调试
  • 「SAP ABAP」OPEN SQL(七)【GROUP BY | HAVING | ORDER BY】
  • 容器-LinkedList
  • Flask 路由和视图函数
  • Linux主机 SSH 通过密钥登录
  • 中国信息安全测评中心-自主原创测评
  • redis杂谈之部分重同步的实现
  • 多态部分参考答案
  • 【高项】项目人力资源管理,沟通管理与干系人管理(十大管理)
  • Wikijs简介-强大可扩展的开源维基软件
  • 微博舆情分析系统的设计与实现(python)
  • 【AUTOSAR】【Lin通信】LinTrcv
  • UE4C++学习篇(十九)-- 动画蒙太奇初级使用
  • 子集和问题
  • 苹果蓝牙耳机太贵了买哪个替代?苹果蓝牙耳机平替推荐
  • CK-UR05-US桌面式超高频RFID发卡器开发手册之USB控制命令格式