JavaMail发送含有插入图片和表格的邮件

引入依赖  javamail、jcommon、jfreechart

            <javamail-version>1.8.3</javamail-version>
            <jcommon.version>1.0.23</jcommon.version>
            <jfreechart.version>1.0.17</jfreechart.version> 
            <dependency>
                <groupId>org.apache.geronimo.javamail</groupId>
                <artifactId>geronimo-javamail_1.4_provider</artifactId>
                <version>${javamail-version}</version>
            </dependency>
            <dependency>
                <groupId>org.jfree</groupId>
                <artifactId>jcommon</artifactId>
                <version>${jcommon.version}</version>
            </dependency>
            <dependency>
                <groupId>org.jfree</groupId>
                <artifactId>jfreechart</artifactId>
                <version>${jfreechart.version}</version>
            </dependency>

创建邮件发送模板,delay-mail.vm

<!DOCTYPE html>
<html lang="zh">
<head>
    <META http-equiv=Content-Type content=‘text/html; charset=UTF-8‘>
    <title>Title</title>
    <style type="text/css">
        table.reference, table.tecspec {
            border-collapse: collapse;
            width: 100%;
            margin-bottom: 4px;
            margin-top: 4px;
        }
        table.reference tr:nth-child(even) {
            background-color: #fff;
        }
        table.reference tr:nth-child(odd) {
            background-color: #f6f4f0;
        }
        table.reference th {
            color: #fff;
            background-color: #555;
            border: 1px solid #555;
            font-size: 12px;
            padding: 3px;
            vertical-align: top;
        }
        table.reference td {
            line-height: 2em;
            min-width: 24px;
            border: 1px solid #d4d4d4;
            padding: 5px;
            padding-top: 7px;
            padding-bottom: 7px;
            vertical-align: top;
        }
        .article-body h3 {
            font-size: 1.8em;
            margin: 2px 0;
            line-height: 1.8em;
        }
    </style>
</head>
<body>
<h3 style=";">滞留率分析报表</h3>
<div>
    <table class="reference">
        <tbody>
            <tr>
                <th>仓库</th>
                <th>初始化</th>
                <th>拣货中</th>
                <th>拣货完成</th>
                <th>装箱中</th>
                <th>装箱完成</th>
                <th>交接中</th>
                <th>交接完成</th>
                <th>已出库</th>
                <th>合计</th>
                <th>滞留率</th>
                <th>及时率</th>
            </tr>
        #foreach($element in  $datas)
        <tr>
            <td>
                #if($element.warehouseName)
                    $element.warehouseName
                #end
            </td>
            <td>
                #if($element.countInit)
                    $element.countInit
                #end
            </td>
            <td>
                #if($element.countPicking)
                    $element.countPicking
                #end
            </td>
            <td>
                #if($element.countPickFinish)
                    $element.countPickFinish
                #end
            </td>
            <td>
                #if($element.countPacking)
                    $element.countPacking
                #end
            </td>
            <td>
                #if($element.countPackFinish)
                    $element.countPackFinish
                #end
            </td>
            <td>
                #if($element.countTransferring)
                    $element.countTransferring
                #end
            </td>
            <td>
                #if($element.countTransferFinish)
                    $element.countTransferFinish
                #end
            </td>
            <td>
                #if($element.countDelivery)
                    $element.countDelivery
                #end
            </td>
            <td>
                #if($element.countTotal)
                    $element.countTotal
                #end
            </td>
            <td>
                #if($element.delayRate)
                    $element.delayRate
                #end
            </td>
            <td>
                #if($element.timelyRate)
                    $element.timelyRate
                #end
            </td>
        </tr>
        #end
        </tbody>
    </table>
    <img src="$imgPath" style="width: 100%">
    <div style="float: left; margin-top: 300px;;">
        <p>系统邮件(请勿回复) | 药品技术中心—供应链</p>
    </div>
</div>
</body>
</html>

邮件工具类 MailUtil.java

package com.yyw.scs.util;

import com.yyw.scs.model.Email;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.*;
import java.util.*;

public class MailUtils {

    private static Logger log = Logger.getLogger(MailUtils.class);

    public static final String HTML_CONTENT = "text/html;charset=UTF-8";
    public static final String ATTACHMENT_CONTENT = "text/plain;charset=gb2312";

    private static VelocityEngine velocityEngine;

    public <T extends List> void sendEmail(T t, String title, String[] to, String[] bcc, String templateName) {
        Map map = new HashMap();
        map.put("datas", t);
        Email email = new Email.Builder(title, to, null).model(map).templateName(templateName).bcc(bcc).build();
        sendEmail(email);
    }
    public <T extends List> void sendEmail(T t, String imgPath, String title, String[] to, String[] bcc, String templateName) {
        Map map = new HashMap();
        map.put("datas", t);
        map.put("imgPath", imgPath);
        Email email = new Email.Builder(title, to, null).model(map).templateName(templateName).bcc(bcc).build();
        sendEmail(email);
    }

    public <T extends List> void sendEmail(String title, String[] to, String[] bcc, String templateName, Map<String, Object> data) {
        Email email = new Email.Builder(title, to, null).model(data).templateName(templateName).bcc(bcc).build();
        sendEmail(email);
    }

    public void sendEmail(String title, String[] to, String[] bcc, String content, byte[] data, String fileName, String fileType) {
        Email email = new Email.Builder(title, to, content).bcc(bcc).data(data).fileName(fileName).fileType(fileType).build();
        sendEmail(email);
    }

    public <T extends List> void sendEmail(String title, String[] to, String[] bcc, String templateName, byte[] data, String fileName, String fileType, Map map) {
        Email email = new Email.Builder(title, to, null).model(map).templateName(templateName).bcc(bcc).data(data).fileType(fileType).fileName(fileName).build();
        sendEmail(email);
    }

    public <T extends List> void sendEmail(T t, String title, String[] to, String[] bcc, String templateName, byte[] data, String fileName, String fileType) {
        Map map = new HashMap();
        map.put("datas", t);
        Email email = new Email.Builder(title, to, null).model(map).templateName(templateName).bcc(bcc).data(data).fileType(fileType).fileName(fileName).build();
        sendEmail(email);
    }

    public void sendEmail(String title, String[] to, String content) {
        Email email = new Email.Builder(title, to, content).bcc(null).build();
        sendEmail(email);
    }

    public void sendEmail(String title, String[] to, String[] bcc, String content) {
        Email email = new Email.Builder(title, to, content).bcc(bcc).build();
        sendEmail(email);
    }

    //发送html模板邮件
    private void sendEmail(Email email) {
        Long startTime = System.currentTimeMillis();
        // 发件人
        try {
            MimeMessage message = this.getMessage(email);
            // 新建一个存放信件内容的BodyPart对象
            Multipart multiPart = new MimeMultipart();
            MimeBodyPart mdp = new MimeBodyPart();
            // 给BodyPart对象设置内容和格式/编码方式
            setContent(email);
            mdp.setContent(email.getContent(), HTML_CONTENT);
            multiPart.addBodyPart(mdp);
            // 新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
            if (null != email.getData()) {
                MimeBodyPart attchment = new MimeBodyPart();
                ByteArrayInputStream in = new ByteArrayInputStream(email.getData());
                DataSource fds = new ByteArrayDataSource(in, email.getFileType());
                attchment.setDataHandler(new DataHandler(fds));
                attchment.setFileName(MimeUtility.encodeText(email.getFileName()));
                multiPart.addBodyPart(attchment);
                if (in != null) {
                    in.close();
                }
            }
            message.setContent(multiPart);
            message.saveChanges();
            Transport.send(message);
            Long endTime = System.currentTimeMillis();
            log.info("邮件发送成功 耗时:" + (endTime - startTime) / 1000 + "s");
        } catch (Exception e) {
            log.error("Error while sending mail.", e);
        }
    }

    private Email setContent(Email email) {
        if (StringUtils.isEmpty(email.getContent())) {
            email.setContent("");
        }
        if (StringUtils.isNotEmpty(email.getTemplateName()) && null != email.getModel()) {
            String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, email.getTemplateName(), "UTF-8", email.getModel());
            email.setContent(content);
        }
        return email;
    }

    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }

    private MimeMessage getMessage(Email email) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "10.6.8.19");
        props.put("mail.smtp.auth", "true");
        props.put("username", "gyl_sys");
        props.put("password", "wms,123456");
        // 发件人
        MimeMessage message = null;
        try {
            if (email.getTo() == null || email.getTo().length == 0 || StringUtils.isEmpty(email.getSubject())) {
                throw new Exception("接收人或主题为空");
            }
            InternetAddress fromAddress = new InternetAddress("[email protected]");
            // toemails(字符串,多个收件人用,隔开)
            Session mailSession = Session.getDefaultInstance(props, new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("gyl_sys", "wms,123456");
                }
            });
            message = new MimeMessage(mailSession);
            message.setFrom(fromAddress);
            for (String mailTo : email.getTo()) {
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
            }
            List<InternetAddress> ccAddress = new ArrayList<>();
            if (null != email.getBcc()) {
                for (String mailCC : email.getBcc()) {
                    ccAddress.add(new InternetAddress(mailCC));
                }
                message.addRecipients(Message.RecipientType.CC,
                        ccAddress.toArray(new InternetAddress[email.getBcc().length]));
            }
            message.setSentDate(new Date());
            message.setSubject(email.getSubject());
        } catch (Exception e) {
            log.error("Error while sending mail." + e.getMessage(), e);
        }
        return message;
    }

}

绘制图表以及发送 SendDelayMailJobServiceImpl.java

package com.yyw.scs.service.impl;

import com.yyw.scs.model.request.DelayResultDto;
import com.yyw.scs.service.SendDelayMailJobService;
import com.yyw.scs.util.DiamondScsConfig;
import com.yyw.scs.util.MailUtils;
import com.yyw.scs.wms.service.DelayRateService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

@Service("sendDelayMailJobService")
public class SendDelayMailJobServiceImpl implements SendDelayMailJobService {

    @Autowired
    private MailUtils mailUtils;

    @Autowired
    private DelayRateService delayRateService;

    private final static Logger LOGGER = Logger.getLogger(SendDelayMailJobServiceImpl.class);

    @Override
    public Boolean send() {
        try {

            // 1. 得到数据
            List<DelayResultDto> res = delayRateService.findDelayResultDtoList(null, null, null, 1);
            if (!CollectionUtils.isNotEmpty(res)) {
                LOGGER.info("SendDelayMailJobServiceImpl.send nodata:");
                return null;
            }
            CategoryDataset dataset = getDataSet(res);

            // 2. 构造chart
            JFreeChart chart = ChartFactory.createBarChart3D(
                    "仓库履约实时分析", // 图表标题
                    "状态", // 目录轴的显示标签--横轴
                    "数量", // 数值轴的显示标签--纵轴
                    dataset, // 数据集
                    PlotOrientation.VERTICAL, // 图表方向:水平、
                    true, // 是否显示图例(对于简单的柱状图必须
                    false, // 是否生成工具
                    false // 是否生成URL链接
            );
            // 3. 处理chart中文显示问题
            processChart(chart);

            // 4. chart输出图片
            // 输出到本机TOMCAT路径
//            File directory = new File(".");
//            String path = null;
//            try {
//                path = directory.getCanonicalPath();
//            } catch (IOException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//            path += "\\img\\delay.jpg";
            // 输出到本机绝对路径
            String path = "/app/img/";
            File file = new File(path);
            System.out.println(path);
            if (!file.exists() && !file.isDirectory()) {
                file.mkdir();
            }
            path += "delay.jpg";
            writeChartAsImage(chart, path);

            // 5. chart 以swing形式输出
//            ChartFrame pieFrame = new ChartFrame("XXX", chart);
//            pieFrame.pack();
//            pieFrame.setVisible(true);

            // 6.发送邮件
            List<String> toList = DiamondScsConfig.getJobSendDelayToList();
            if (CollectionUtils.isNotEmpty(toList)) {
                String[] toArr = (String[]) toList.toArray(new String[toList.size()]);
                mailUtils.sendEmail(res, path, "滞留率分析报表", toArr, null, "spring/email/delay-mail.vm");
            } else {
                LOGGER.info("发送邮件: 未获取到收件人列表");
            }

        } catch (Exception e) {
            LOGGER.error("SendDelayMailJobServiceImpl.send error:", e);
            // todo 发送报警邮件
            return false;
        }
        return true;
    }

    /**
     * 获取一个演示用的组合数据集对象
     *
     * @return
     */
    private CategoryDataset getDataSet(List<DelayResultDto> res) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (DelayResultDto item : res) {
            dataset.addValue(item.getCountInit(), item.getWarehouseName(), "初始化");
            dataset.addValue(item.getCountPicking(), item.getWarehouseName(), "拣货中");
            dataset.addValue(item.getCountPickFinish(), item.getWarehouseName(), "拣货完成");
            dataset.addValue(item.getCountPacking(), item.getWarehouseName(), "装箱中");
            dataset.addValue(item.getCountPackFinish(), item.getWarehouseName(), "装箱完成");
            dataset.addValue(item.getCountTransferring(), item.getWarehouseName(), "交接中");
            dataset.addValue(item.getCountTransferFinish(), item.getWarehouseName(), "交接完成");
            dataset.addValue(item.getCountDelivery(), item.getWarehouseName(), "已出库");
        }
        return dataset;
    }

    private CategoryDataset getDemoDataSet() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(100, "北京", "苹果");
        dataset.addValue(120, "上海", "苹果");
        dataset.addValue(160, "广州", "苹果");
        dataset.addValue(210, "北京", "梨子");
        dataset.addValue(220, "上海", "梨子");
        dataset.addValue(230, "广州", "梨子");
        dataset.addValue(330, "北京", "葡萄");
        dataset.addValue(340, "上海", "葡萄");
        dataset.addValue(340, "广州", "葡萄");
        dataset.addValue(420, "北京", "香蕉");
        dataset.addValue(430, "上海", "香蕉");
        dataset.addValue(400, "广州", "香蕉");
        dataset.addValue(510, "北京", "荔枝");
        dataset.addValue(530, "上海", "荔枝");
        dataset.addValue(510, "广州", "荔枝");
        return dataset;
    }

    /**
     * 解决图表汉字显示问题
     *
     * @param chart
     */
    private static void processChart(JFreeChart chart) {
        CategoryPlot plot = chart.getCategoryPlot();
        CategoryAxis domainAxis = plot.getDomainAxis();
        ValueAxis rAxis = plot.getRangeAxis();
        chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        TextTitle textTitle = chart.getTitle();
        textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
        domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
        domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
        rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
        rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
        chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
        // renderer.setItemLabelGenerator(new LabelGenerator(0.0));
        // renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));
        // renderer.setItemLabelsVisible(true);
    }

    /**
     * 输出图片
     *
     * @param chart
     */
    private static void writeChartAsImage(JFreeChart chart, String path) {
        FileOutputStream fos_jpg = null;
        try {
            fos_jpg = new FileOutputStream(path);
            ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 800, 500, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos_jpg.close();
            } catch (Exception e) {
            }
        }
    }
}

原文地址:https://www.cnblogs.com/durp/p/9209175.html

时间: 2024-11-10 11:51:01

JavaMail发送含有插入图片和表格的邮件的相关文章

Excel:插入图片随表格隐藏,或显示

今天遇到个问题,在做数据表的时候,插入的图片一直显现,不随当前变化的表格做变化,十分不方便, 在网上查了查,又试了下,找到属性,可以处理这个问题,右键点击,选择大小和属性,大小和位置随单元格而变, 成功.. .. PS:此文仅为记录,不做他用 Excel:插入图片随表格隐藏,或显示

C#发送内置图片的html格式邮件的代码

将写内容过程经常用的内容段备份一次,下面的内容是关于C#发送内置图片的html格式邮件的内容,应该对码农们也有用处.MailMessage m = new MailMessage();m.Subject = "html email with embedded image coming!"; string htmlBody = "<html><body><h1>Picture</h1><br><img src=&

[Java] JavaMail 发送 html 格式、带附件的邮件

本案例演示发送 html 格式,可带附件的邮件发送.发送纯文本邮件的例子可参照上一篇博文JavaMail 简单案例. EmailHelper, Email 的帮助类,向帮助类提供 SMTP 服务器域名.用户名.密码.发送人邮箱.收件人邮箱.邮件主题.html 格式的内容(可选).附件(可选),便可发送一份邮件. SendEmailDemo, 演示发送邮件. import java.util.Properties; import javax.activation.DataHandler; impo

C# 操作Word文本框——插入图片、表格、文字、超链接等

概述 Text Box(文本框)是Word排版的工具之一.在Word文档中的任何地方插入文本框,可添加补充信息,放在合适的位置,也不会影响正文的连续性.我们可以设置文本框的大小,线型,内部边距,背景填充等效果.文本框内可以图文混排,设置字体,字号,图片大小.文字链接,绘入表格等.在下面的示例中,将分为两部分来介绍在Word中插入文本框,分别是:第一部分:插入图文混排的文本框,包含图片填充,内部边距,图文混排.文字超链接等元素第二部分:关于在文本框中插入表格.读取表格.删除表格等操作 使用工具 *

java程序发送内嵌图片、带附件邮件

package mail; import java.io.UnsupportedEncodingException;import java.util.Properties; import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingExc

[Java] JavaMail 发送带图片的 html 格式的邮件

JavaMail 发送的邮件正文和附件是相互独立的,但是内置图片需要定位图片在正文中的位置,所以内置图片和邮件正文是互相依赖的. 发送带附件的邮件可参考JavaMail 发送 html 格式.带附件的邮件. 发送纯文本的邮件可参照 JavaMail 简单案例. 具体例子 EmailHelper, Email 的帮助类,向帮助类提供 SMTP 服务器域名.用户名.密码.发送人邮箱.收件人邮箱.邮件主题.html 格式的内容.图片的路径,便可发送一份内置图片的邮件.在创建 MimeMultipart

C#生成Word报表(插入文字、表格和图片)

回家前一个晚上,老板临时安排让写一个读取txt文件的数据和多个图片数据生成一个word文档.时间给得太短只能是回家继续加班,百度真是个好东西这里引用一下我参考的博客http://blog.csdn.net/jiutao_tang/article/details/6574740/ http://xuzhihong1987.blog.163.com/blog/static/2673158720109188465634/  这个博客讲的用录制word宏的方法做参考很有用.感谢这位博友 静下心,认真做,

【excel技巧读书笔记002】批注插入图片表格

               今天看到学到一个excel小技巧,批注插入图片表格,觉得挺好分享给大家.

MVC 生成PDf表格并插入图片

最近做的项目中有一个功能,将最终的个人信息生成PDF表格,并插入图片.对于没接触过的程序员来说回一片茫然,网上有多种生成PDf的方法,我给大家介绍一下我认为比较简单,好操作的一种. iTextSharp组件 使用方法. 1,引入itextsharp.dll 2,  引入命名空间 ? 1 2 3 using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf; ? 1 2 3 4 5 6 7 8 9 10 11 12     D