String to = "xxx@qq.com"; // 收件人的QQ邮箱
String from = "xxx@qq.com"; // 发件人的QQ邮箱
String authoCode = "xxx"; // 使用qq邮箱发送时的授权码
String smtpServer = "smtp.qq.com";

/**
 * 1,创建邮件对象
 */
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpServer); // 指定SMTP服务器
properties.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证
Session session = Session.getInstance(properties);
MimeMessage message = new MimeMessage(session);

try {
    /**
     * 2. 设置发件人
     * 其中 InternetAddress 的三个参数分别为: 邮箱, 显示的昵称(只用于显示, 没有特别的要求), 昵称的字符集编码
     */
    message.setFrom(new InternetAddress(from, "XXX", "UTF-8"));

    /**
     * 3,设置收件人
     * To收件人   CC 抄送  BCC密送
     */
    message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(to, "", "UTF-8"));
    message.addRecipient(MimeMessage.RecipientType.TO,new InternetAddress(to, "", "UTF-8"));

    /**
     * 4,设置标题
     */
    message.setSubject("报表","UTF-8");

    /**
     * 5,组织数据
     */
    Workbook xlsFile = new HSSFWorkbook(); // create a workbook
    CreationHelper helper = xlsFile.getCreationHelper();

    Sheet sheet1 = xlsFile.createSheet("title"); // add a sheet to your workbook
    org.apache.poi.ss.usermodel.Row row1 = sheet1.createRow((short)0);
    sheet1.setColumnWidth(0, 25 * 256);
    sheet1.setColumnWidth(1, 30 * 256);
    row1.createCell(0).setCellValue(helper.createRichTextString("cell0"));
    row1.createCell(1).setCellValue(helper.createRichTextString("cell1"));
    row1.createCell(2).setCellValue(helper.createRichTextString("cell2"));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    xlsFile.write(bos); // write excel data to a byte array
    bos.close();
    DataSource fds = new ByteArrayDataSource(bos.toByteArray(), "application/vnd.ms-excel");

    MimeBodyPart mbp1 = new MimeBodyPart();
    mbp1.setText("Everything will be fine.");

    MimeBodyPart mbp2 = new MimeBodyPart();
    mbp2.setDataHandler(new DataHandler(fds));
    mbp2.setFileName("report.xls");

    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp1);
    mp.addBodyPart(mbp2);
    message.setContent(mp);

    /**
     * 6,保存邮件并发送
     */
    message.saveChanges();
    Transport transport = session.getTransport("smtp");
    transport.connect(smtpServer, from, authoCode);
    transport.sendMessage(message,message.getAllRecipients());
    transport.close();

} catch (MessagingException e) {
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
  • 需要使用到的pom依赖

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

Java以邮件附件的方式发送excel文件的更多相关文章

  1. kettle文件以邮件附件的形式发送报告

    将从表中导出的excel文件以邮件附件的形式发送报告 step1: 导出文件file1.xls step2: add filename to result将文件添加到结果 step3: 发送邮件

  2. 【转载】WPS通过设置密码的方式对Excel文件加密

    有时候Excel文件中可能包含一些敏感数据,此时希望对Excel文件进行加入密码的形式进行加密保护,在WPS软件和Office Excel软件中都支持对Excel文件进行密码保护,设置了密码保护的Ex ...

  3. Java中使用POI读取大的Excel文件或者输入流时发生out of memory异常参考解决方案

    注意:此参考解决方案只是针对xlsx格式的excel文件! 背景 前一段时间遇到一种情况,服务器经常宕机,而且没有规律性,查看GC日志发生了out of memory,是堆溢出导致的,分析了一下堆的d ...

  4. C# Aspose.Cells方式导入Excel文件

    读取Excel 类 我返回的是DataTable 类型 也可以返回DataSet类型 public class XlsFileHelper { public DataTable ImportExcel ...

  5. 利用freemarker+SAX解析xml的方式对excel文件字段校验

    利用freemarker对参数进行校验这篇文章主要用到的技术点: 自定义注解的使用反射机制SAX解析xmlFreemarker的运用我们在工作中经常需要上传excel文件,然后在对文件中的字段进行校验 ...

  6. oledb方式读取excel文件

    进入博客园后台发现12年11月份写的草稿没发,时隔1年,把它拉出来晒晒太阳. 前言 第一次做Excel文件导入,采用了oledb,不足之处,还请各位大牛指出,谨以此文对导入Excel做个总结. 一般步 ...

  7. Java实现将任何编码方式的txt文件以UTF-8编码方式转存

    本文利用JDK中的BufferedReader和BufferedWriter实现将任何编码方式的txt文件以UTF-8编码方式转存. UTF-8(8-bit Unicode Transformatio ...

  8. 在Delphi中通过OLE方式写Excel文件

    报表的打印是每个项目都会遇到的问题.由于报表格式要求五花八门,往往又同时要求打印格式可方便调整.作为一种替代方法,可以将需要打印的报表导出到Excel/Word,打印交给Office去吧.由于Offi ...

  9. java poi 读取有密码加密的Excel文件

    String excelPath = "Excel文件路徑"; String password = "Excel文件密碼"; Workbook workbook ...

随机推荐

  1. Spring Cloud与分布式系统

    本文不是讲解如何使用spring Cloud的教程,而是探讨Spring Cloud是什么,以及它诞生的背景和意义. 背景 2008年以后,国内互联网行业飞速发展,我们对软件系统的需求已经不再是过去” ...

  2. Android 4 学习(15):持久化:Files, Saving State and Preferences

    参考<Professional Android 4 Development> 持久化:Files, Saving State and Preferences Android中的数据持久化 ...

  3. <正则吃饺子> :关于redis集群的搭建、集群测试、搭建中遇到的问题总结

    项目中使用了redis ,对于其基本的使用,相对简单些,根据项目中已经提供的工具就可以实现基本的功能,但是只是这样的话,对于redis还是太肤浅,甚至刚开始时候,集群.多节点.主从是什么,他们之间是什 ...

  4. git用法小结(1)--建立远程仓库

    最近一直在学习使用git来管理自己的程序,总是今天东学一点,明天西凑一点,到用的时候,总是有些茫然不知所措. 在博客园里看见一篇老好的文章,教我们做笔记啦,但是做完笔记还是要记得总结哦! 来吧,让我们 ...

  5. NetworkView

    [游戏Server中Server的类别] There are two common and proven approaches to structuring a network game which ...

  6. PHP网站

    1.NetBeans 解决PHP调试问题:https://netbeans.org/ 支持PHP调试的版本 2. http://document.thinkphp.cn/manual_3_2.html ...

  7. opennebule 创建cdrom数据发送

    {","csrftoken":"b9b5026f1a92180b789971ed8e21d28b"}

  8. Solidity string to uint

    oraclize result以string格式返回,solidity没有uint(string)这样的强制转换功能,如果要解析其中的数字,可以用oraclize提供的parseInt方法: prag ...

  9. c语言实践 1/1-1/2+1/3-1/4+...

    其实这个题目和上面那个是一样的 /* 1/1-1/2+1/3-1/4+...1/n; */ int n = 1; double sum = 0; double frac = 0; int i = 1; ...

  10. 2、awk的输出

    1.常见的输出格式整理 awk '{print "this is " $1, $2, $1*$2, NR, NF, $NF}' file1   ###字符输出,字段输出,运算输出, ...