Hello,
My application receives mails sent from some different customers and parses the mail's attachments. The problem is that all these customers send their mails in a very different ways. Some of them send mails with "Content-Type: multipart/mixed;" header while some others send mail with the following headers (among some other):
MIME-Version: 1.0
Content-Type: application/xml
Date: Tue, 11 Feb 2014 18:33:26 +0000
From: <XXX@XXX.XX>
Message-ID: <1392143606.220.1@cls0361>
Subject: orders000005311382.xml
To: <myaddress@company.com>
There is no "Content-Type: multipart/mixed;" header at all. When I look at these messages with MS Outlook I can see that they have proper attachements.
Now I have 2 problems:
1. I would like to produce such an email myself since resending the received emails changes the message headers. There are lots of examples how to send a message with an attachment in Java with MimeMultipart object (this produces "Content-Type: multipart/mixed;") but I could not find one which sends the message with "Content-Type: application/xml" header only without any multipart header. I suspect that there are some specific applications which send mails in that way - not standard mail clients.
2. I would like to get the attachment of a received mail with "Content-Type: application/xml" header. The following code works for "Content-Type: multipart/mixed;" only:
public void invoke() throws Exception {
/* Available Variables: DO NOT MODIFY
In : String mimeMessage
In : String encoding
Out : String[] mimeParts
* Available Variables: DO NOT MODIFY *****/
javax.mail.util.ByteArrayDataSource ds = new javax.mail.util.ByteArrayDataSource(mimeMessage, "multipart/mixed");
javax.mail.internet.MimeMultipart mm = new javax.mail.internet.MimeMultipart(ds);
java.util.ArrayList<String> al = new java.util.ArrayList<String>();
getAttachements(mm, al, 1);
mimeParts = new String[0];
mimeParts = al.toArray(mimeParts);
}
private void getAttachements(javax.mail.internet.MimeMultipart mm, ArrayList<String> al, int depth) throws Exception {
if (depth > 9) {
return;
}
int mimePartsCount = mm.getCount();
for (int i = 0; i < mimePartsCount; i++){
javax.mail.internet.MimeBodyPart mbp = (javax.mail.internet.MimeBodyPart)mm.getBodyPart(i);
//System.out.println(depth + ": " + mbp + " " + mbp.getContentType() + " " + mbp.getContentID() + " " + mbp.getDisposition());
if(mbp.getContentType().startsWith("multipart")){
javax.mail.util.ByteArrayDataSource ds2 = new javax.mail.util.ByteArrayDataSource(mbp.getInputStream(), "multipart/mixed");
javax.mail.internet.MimeMultipart mm2 = new javax.mail.internet.MimeMultipart(ds2);
getAttachements(mm2, al, depth+1);
} else {
if (mbp.getDisposition() != null && mbp.getDisposition().startsWith("attachment") && !mbp.getContentType().contains("signature")){
BufferedReader br = new BufferedReader(new InputStreamReader(mbp.getInputStream(),encoding));
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {
sb.append(s);
s = br.readLine();
}
al.add(sb.toString());
}
}
}
}
Could anyone suggest anything which may be helpful for resolving 1 or 2?
Best regards