java来接收邮件并解析邮件正文中的表格

这里是实际需求中的一个DEMO

有一部分内容进行了注释和处理,参考需要修改成自己的实际参数。另这个是对于实际一个场景的案例并不是通用解决的工具类。

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import static java.lang.System.out;

/**
 * 描述:把邮件转换成此类的一个实例进行处理
 */

/**
 * <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
 * <dependency>
 * <groupId>javax.mail</groupId>
 * <artifactId>mail</artifactId>
 * <version>1.4.7</version>
 * </dependency>
 * <p>
 * <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
 * <dependency>
 * <groupId>org.jsoup</groupId>
 * <artifactId>jsoup</artifactId>
 * <version>1.12.1</version>
 * </dependency>
 */
public class ReceiveOneMail {

    /**
     * 邮件信息
     */
    private MimeMessage mimeMessage;
    /**
     * 邮件正文内容
     */
    private StringBuffer bodyText = new StringBuffer();

    public ReceiveOneMail(MimeMessage mimeMessage) {
        this.mimeMessage = mimeMessage;
    }

    /**
     * main测试方法
     */
    public static void main(String args[]) throws Exception {

        Message[] messages = testInit();
        
        ReceiveOneMail pmm = null;
        // 循环测试邮件的收件箱邮件【时间倒序】
        for (int i = messages.length; i >= 0; i--) {
            //            // 构建 ReceiveOneMail 实例
            pmm = new ReceiveOneMail((MimeMessage) messages[i]);
            out.println("---------" + pmm.getSubject() + "--------");
            pmm.getMailContent((Part) messages[i]);
            List<MailContentTableInfo> list = pmm.parseContent();
            // 模拟输出解析结果
            for (MailContentTableInfo mailContentTableInfo :
                    list) {
                out.println(mailContentTableInfo.toString());
            }
        }
    }

    /**
     * 初始化构建一个连接邮箱的信息进行测试
     *
     * @return 一个数组邮件信息
     * @throws MessagingException 邮件信息异常
     */
    public static Message[] testInit() throws MessagingException {
        Properties props = System.getProperties();
        props.put("mail.pop3.host", "【实际值】");
        Session session = Session.getDefaultInstance(props, null);
        URLName url = new URLName("pop3", "【实际值】", 110, null,
                "【邮箱地址】", "【邮箱密码】");
        Store store = session.getStore(url);
        store.connect();
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        return folder.getMessages();
    }

    /**
     * 处理邮件中的正文,解析出里面的表格【第一个表格】
     * 按照【委托资金划拨】的规则进行解析并实例化为一组MailContentTableInfo
     */
    public List<MailContentTableInfo> parseContent() throws MessagingException, ParseException {
        List<MailContentTableInfo> list = new ArrayList<>();
        Document doc = Jsoup.parse(this.getBodyText());
        // 处理匹配到的第一个 table 表格,并且获得表格中的所有行
        Elements rows = doc.select("table").get(0).select("tr");
        Elements firstRowCols = rows.get(0).select("td");
        Elements lastRowCols = rows.get(rows.size() - 1).select("td");
        // 处理整个表格的业务时间
        String transferDate = evalTransferDate(rows.get(1).select("td").get(0).text());
        for (int i = 1; i < firstRowCols.size(); i++) {
            MailContentTableInfo mailContentTableInfo = new MailContentTableInfo();
            mailContentTableInfo.setMailDate(this.getSentDate());
            mailContentTableInfo.setTransferDate(transferDate);
            mailContentTableInfo.setProductName(firstRowCols.get(i).text().replace(" ", ""));
            mailContentTableInfo.setTransferAmount(new BigDecimal(lastRowCols.get(i).text().replace(",", "")));
            list.add(mailContentTableInfo);
        }
        return list;
    }

    /**
     * 处理表格中的业务日期,和邮件发送时间的年份拼接成8位的字符日期;
     * 原有表格中的日期是 6月2日这种,所以需要特殊处理。
     */
    private String evalTransferDate(String transferDateText) throws MessagingException {
        String[] transferMonthDate = transferDateText.split("月");
        String month = transferMonthDate[0].length() == 1 ? "0" + transferMonthDate[0] : transferMonthDate[0];
        String[] transferDayDate = transferMonthDate[1].split("日");
        String day = transferDayDate[0].length() == 1 ? "0" + transferDayDate[0] : transferDayDate[0];
        return this.getSentYearDate() + month + day;
    }

    /**
     * 获得邮件发送日期
     */
    private String getSentDate() throws MessagingException {
        return new SimpleDateFormat("yyyyMMdd").format(mimeMessage.getSentDate());
    }

    /**
     * 获得邮件发送年
     */
    private String getSentYearDate() throws MessagingException {
        return this.getSentDate().substring(0, 4);
    }

    /**
     * 获得当前邮件主题
     */
    private String getSubject() throws MessagingException, UnsupportedEncodingException {
        String subject = MimeUtility.decodeText(mimeMessage.getSubject());
        return subject == null ? "" : subject;
    }

    /**
     * 获得邮件正文内容
     */
    private String getBodyText() {
        return bodyText.toString();
    }

    /**
     * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析
     */
    private void getMailContent(Part part) throws Exception {
        String contentType = part.getContentType();
        int nameIndex = contentType.indexOf("name");
        boolean conName = false;
        if (nameIndex != -1) {
            conName = true;
        }
        if (part.isMimeType("text/plain") && !conName) {
            bodyText.append((String) part.getContent());
        } else if (part.isMimeType("text/html") && !conName) {
            bodyText.append((String) part.getContent());
        } else if (part.isMimeType("multipart/*")) {
            Multipart multipart = (Multipart) part.getContent();
            int counts = multipart.getCount();
            for (int i = 0; i < counts; i++) {
                getMailContent(multipart.getBodyPart(i));
            }
        } else if (part.isMimeType("message/rfc822")) {
            getMailContent((Part) part.getContent());
        }
    }
}

/**
 * 定义解析邮件表格内容后的实体类
 */
class MailContentTableInfo {

    private String col1;
    private String col2;
    private String col3;
    private BigDecimal col4;

    public String getMailDate() {
        return col1;
    }

    public void setMailDate(String mailDate) {
        this.col1 = mailDate;
    }

    public String getProductName() {
        return col3;
    }

    public void setProductName(String productName) {
        this.col3 = productName;
    }

    public BigDecimal getTransferAmount() {
        return col4;
    }

    public void setTransferAmount(BigDecimal transferAmount) {
        this.col4 = transferAmount;
    }

    public String getTransferDate() {
        return col2;
    }

    public void setTransferDate(String transferDate) {
        this.col2 = transferDate;
    }

    @Override
    public String toString() {
        return "MailContentTableInfo{" +
                "col1=" + col1 +
                ", col2=" + col2 +
                ", col3='" + col3 + '\'' +
                ", col4=" + col4 +
                '}';
    }
}
posted @ 2019-09-23 17:15  LoveSnowBaby  阅读(1495)  评论(0编辑  收藏  举报