*
* This method processes the MIME data and returns the message object
*
*/
public Message parseMessage(String messageString,String locURI) {
Message msg = new Message();
Hashtable ht = new Hashtable();
try {
messageString = extractMIMEContent(messageString);
ht = CreateHashTable(messageString);
MailContent mailContent = new MailContent((String)ht.get("CONTENT-TYPE"));
mailContent.parseContent();
MailBody body = mailContent.getMailBody();
Vector attachment = mailContent.getAttachmentList();
msg.addHeader("FROM", (String)ht.get("FROM"));
msg.addHeader("TO", (String)ht.get("TO"));
msg.setHeader("DATE", (String)ht.get("DATE"));
msg.setSentDate(parseRfc822Date((String)ht.get("DATE")));
// Set sent date, shown when message is opened
msg.addHeader("MIME-Version", (String)ht.get("MIME-VERSION"));
// FIXME: should be set the received date too, if possible.
MessageIDStore msgIdStore = new MessageIDStore();
msgIdStore.setMessageMappingKey();
msgIdStore.add(locURI, (String)ht.get("MESSAGE-ID"));
msg.setSubject((String)ht.get("SUBJECT"));
if ((String)ht.get("FROM") != null) {
String strFrom = (String)ht.get("FROM");
if(strFrom.indexOf("<") >= 0){
strFrom = strFrom.substring(strFrom.indexOf("<") + 1, strFrom.indexOf(">"));
}
Address from = new Address(strFrom.trim(), " ");
msg.setFrom(from);
}
// FIXME: What if there is more than one To: ??
if ((String)ht.get("TO") != null) {
String strTo = (String)ht.get("TO");
if (strTo.indexOf("<") >= 0) {
strTo = strTo.substring(strTo.indexOf("<") + 1, strTo.indexOf(">"));
}
Address to[] = new Address[1];
to[0] = new Address(strTo.trim(), " ");
msg.addRecipients(Message.RecipientType.TO, to);
}
/*
while (strTo.indexOf("<") >= 0) {
addr = strTo.substring(strTo.indexOf("<") + 1, strTo.indexOf(">"));
Arrays.add(to, new Address(addr.trim(), " "));
}
msg.addRecipients(Message.RecipientType.TO, to);
*/
if (attachment.size() == 0) {
if (body.getActualContent() != null) {
String encoding = (String)ht.get("CONTENT-TRANSFER-ENCODING");
if (encoding != null && encoding.trim().equalsIgnoreCase("base64")) {
msg.setContent(new String(Base64.decode(body.getActualContent().getBytes())));
} else {
msg.setContent(body.getActualContent());
}
}
} else {
Multipart mp = new Multipart();
TextBodyPart bodypart = new TextBodyPart(mp);
int bodyPartIndex = 0;
if (body.getActualContent() != null) {
String encoding = body.getContentTransferEncoding();
bodypart.setContentType(body.getContentType());
String content = null;
if (encoding != null && encoding.trim().equalsIgnoreCase("base64")){
content = new String(Base64.decode(body.getActualContent().getBytes()));
} else {
content = body.getActualContent();
}
bodypart.setContent(content);
mp.addBodyPart(bodypart,bodyPartIndex);
++bodyPartIndex;
}
for (int i = 0; i < attachment.size(); i++) {
SupportedAttachmentPart sp = new SupportedAttachmentPart(mp);
MailAttachment mailAttachment = (MailAttachment)attachment.elementAt(i);
sp.setFilename(mailAttachment.getFileName());
if (mailAttachment.getContentTransferEncoding().equals("base64")){
sp.setContent(new String(Base64.decode(mailAttachment.getactualContent().getBytes())));
}
else {
sp.setContent(mailAttachment.getactualContent());
}
sp.setContentType(mailAttachment.getContentType());
mp.addBodyPart(sp,bodyPartIndex);
++bodyPartIndex;
}
msg.setContent(mp);
}
} catch(FolderNotFoundException f) {
StaticDataHelper.log("FolderNotFoundException in MIMEParser.parseMessage(): "
+ f.getMessage());