无法从java邮件中的多部分邮件中获取自定义标头

时间:2017-02-13 14:01:13

标签: java javamail multipart

设置标题的代码

Message msg = new MimeMessage(session);

if (fromName != null) {
    addressFrom = new InternetAddress(fromAddress, fromName);
} else {
    addressFrom = new InternetAddress(fromAddress);
}

msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[1];

addressTo[0] = new InternetAddress(recipient.trim().replace(" ", "+"));

msg.setRecipients(Message.RecipientType.TO, addressTo);

msg.addFrom(InternetAddress.parse(from));

msg.setSubject(subject);

InternetAddress[] replyTo = new InternetAddress[1];

replyTo[0] = new InternetAddress(replyAddress.trim().replace(" ", "+"));

msg.setReplyTo(replyTo);

Multipart multipart = new MimeMultipart("alternative");

if (plainMsg != null) {
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(plainMsg, "text/plain ; charset=UTF-8");
    multipart.addBodyPart(messageBodyPart);
}

BodyPart messageBodyPart = new PreencodedMimeBodyPart("base64");
messageBodyPart.setHeader("charset", "utf-8");
messageBodyPart.setContent(htmlMsg, "text/html ; charset=UTF-8");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
msg.setHeader("Precedence", "bulk");
msg.addHeader("trackerid", trackerId);
msg.addHeader("emailMarketingid", emailMarketingid);

我添加了trackerid和emailMarketingid作为标题。他们也出现在gmail邮件标题中,但在获取收件箱时我无法获取这些标题。 在代码下方获取标题 -

                Object content = message.getContent();
                String line = null;
                String bounceStatus = "";
                String bounceReason = "";
                String trackerid = "";
                String emailMarketingid = "";
                BufferedReader bufReader = null;
                if (content instanceof String) {
                    bufReader = new BufferedReader(new StringReader(message.getContent().toString()));
                    while ((line = bufReader.readLine()) != null) {
                        if (line.contains("trackerid")) {
                            trackerid = line.split(":")[1];
                        }
                        if (line.contains("emailMarketingid")) {
                            emailMarketingid = line.split(":")[1];
                        }
                        bounceStatus = get_status_code_from_text(line, bounceStatus);
                        bounceReason = line;
                    }
                } else if (content instanceof Multipart) {
                    MimeMultipart mp = (MimeMultipart) (Multipart) content;
                    for (int j = 0; j < mp.getCount(); j++) {
                        bufReader = new BufferedReader(new StringReader(getTextFromMimeMultipart(mp)));
                        while ((line = bufReader.readLine()) != null) {
                            if (line.contains("trackerid")) {
                                trackerid = line.split(":")[1];
                            }
                            if (line.contains("emailMarketingid")) {
                                emailMarketingid = line.split(":")[1];
                            }
                            bounceStatus = get_status_code_from_text(line, bounceStatus);
                            bounceReason = line;
                        }
                    }
                }


String result = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
    BodyPart bodyPart = mimeMultipart.getBodyPart(i);
    if (bodyPart.isMimeType("text/plain")) {
        result = result + "\n" + bodyPart.getContent();
        break; // without break same text appears twice in my tests
    } else if (bodyPart.isMimeType("text/html")) {
        String html = (String) bodyPart.getContent();
        result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
    }else if (bodyPart.isMimeType("multipart/alternative")) {

        Multipart mp = (Multipart) bodyPart.getContent();
        int partsCount = mp.getCount();
        for(int j=0;j<partsCount;j++){
            result= result +(String) mp.getBodyPart(j).getContent();
            Enumeration e = mp.getBodyPart(j).getAllHeaders();
            while (e.hasMoreElements()) {
                Header h = (Header) e.nextElement();
                System.out.println("Name : " + h.getName() + " Value : " + h.getValue());
            }
        }
    }else if (bodyPart.isMimeType("multipart/mixed")) {

        Multipart mp = (Multipart) bodyPart.getContent();
        int partsCount = mp.getCount();
        result= result +(String) mp.getBodyPart(partsCount - 1).getContent();

    }else if (bodyPart.getContent() instanceof MimeMultipart) {
        result = result + getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
    }
}

2 个答案:

答案 0 :(得分:1)

尝试枚举所有多部分内容类型的标头。

  

bodyPart.isMimeType( “多部分/ *”)

Enumeration headers = part.getAllHeaders();
while (headers.hasMoreElements()) {
    Header h = (Header) headers.nextElement();
        if ("trackerid".equals(h.getName())) {
            log.info(h.getName() + ": " + h.getValue());
        }
    }

答案 1 :(得分:0)

标题不在邮件内容中。使用getHeader方法阅读标题。