user3816288
user3816288

Reputation: 3

Cannot get custom headers from multipart message in java mail

Code to set headers

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);

I have added trackerid and emailMarketingid as a header.They are also appearing in gmail message header, but while fetching inbox i am not able to get these headers. Below Code to fetch headers -

                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());
    }
}

Upvotes: 0

Views: 1272

Answers (2)

arun_hareesh
arun_hareesh

Reputation: 68

Try Enumerating the header for all multipart content type.

bodyPart.isMimeType("multipart/*")

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

Upvotes: 1

Bill Shannon
Bill Shannon

Reputation: 29971

The headers aren't in the message content. Use the getHeader method to read the headers.

Upvotes: 0

Related Questions