javaEmail发送邮件(将导出的excel以邮件的形式发送)
-
导入相关jar包
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.activation/activation --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency>
-
编写工具类
/** * 工具类 * * @author lxx * @date 2018-11-28 */ public class SendMailUtil { /** * 收件人电子邮箱 */ public final static String to = "填写收件人的邮箱"; /** * 发件人电子邮箱 */ public final static String from = "填写发件人的有些"; /** * 指定发送邮件的主机为 smtp.qq.com * QQ 邮件服务器 */ public final static String host = "smtp.qq.com"; /** * 邮箱授权码 */ public final static String pwd = "下面会讲到如果获取到"; /** * * @param subject 标题 * @param content 正文 * @param fileName 文件名称 * @param is 文件输入流 * @return * @throws GeneralSecurityException */ public final static String sendExcel(String subject,String content, String fileName, InputStream is) throws GeneralSecurityException { // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf); // 获取默认session对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { //发件人邮件用户名、密码 return new PasswordAuthentication(from, pwd); } }); try { // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject(subject); // 创建多重消息 Multipart multipart = new MimeMultipart(); // 创建消息部分 BodyPart messageBodyPart = new MimeBodyPart(); // 消息 messageBodyPart.setText(content); // 设置文本消息部分 multipart.addBodyPart(messageBodyPart); /*添加附件*/ if (is != null) { MimeBodyPart fileBody = new MimeBodyPart(); DataSource source = new ByteArrayDataSource(is, "application/msexcel"); fileBody.setDataHandler(new DataHandler(source)); // 中文乱码问题 fileBody.setFileName(MimeUtility.encodeText(fileName)); multipart.addBodyPart(fileBody); } // 发送完整消息 message.setContent(multipart); // 发送消息 Transport.send(message); System.out.println("Sent message successfully....from runoob.com"); return "邮件发送成功"; } catch (MessagingException mex) { mex.printStackTrace(); return "邮件发送失败"; } catch (IOException e) { e.printStackTrace(); return "Io异常"; } } }
-
qq邮箱如何获取授权码
https://jingyan.baidu.com/album/c275f6ba61c583e33d756735.html?picind4,
4.获取excel
XSSFWorkbook book = new XSSFWorkbook(inputStream); //此处省略对excel的操作 /***每次都需要新建输入流 防止发送给第二个用户的时候 邮件内容为空*/ /******workBook写入输出流**/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); book.write(baos); baos.flush(); byte[] bt = baos.toByteArray(); InputStream is = new ByteArrayInputStream(bt, 0, bt.length); SendMailUtil.sendExcel("第四封邮件","测试一下","统计.xlsx",is);