I am new to Apache camel. we are using Apache camel 2.11.0 to poll a microsoft exchange Indox, using POP3 as FROM endpoint and created a bean class to process the email message in the route before sending it to target endpoint. The issue i have is MailMessage object return from Exchange.getIn(MailMessage.class).getMessage() in Processor method does not have any email headers. When i put a Debug option on FROM endpoint i can see the email headers of receiving email in the Apache camel trace. Your help is much appreciated.
Route Config
<!-- Route email exchange-->
<route id="gateMail">
<from uri="pop3://[email protected]?password=MyPassword&debugMode=true&contentType=multipart/alternative" />
<bean ref="MailProcessor" />
<to uri="file:D:/Mail" /></route>
Output in console of Tomcat v7.0 Server at localhost [Apache Tomcat] c:\Program Files\Java\jdk1.7.0_21\bin\Javaw.exe
DEBUG POP3: pipeline message size 2243
S: +OK
Received: by MyMailServer.com id <[email protected]>; Wed, 15 Jan 2014 10:52:21 +1100
x-protective-marking: VER=2005.6, NS=com.au, SEC=UNCLASSIFIED:AUDIT, [email protected]
x-titus-version: 3.3.8.1
x-tituslabs-classifications-30: TLPropertyRoot=Titus;Classification=UNCLASSIFIED;Precedence=ROUTINE;Privacy=AUDIT;
x-tituslabs-classificationhash-30: YifyyqlCDo9l8ySQLTGzHX5c15nHvPqznjIb8keFNu8UEVGWAssd6R0LjeoelHoKfBmfwLv3tDicWe12NsuQFLTSJLlcdr56aL59f1GzpTBosa6XLoHlIutAnNQcPA+xRA5fzirrVFmPv2jTtl8Bqp30thtdKQvwpzqD/KcGI3vS033ldtUOzAF1+/gwx9zL+SBDwxSwUor1ihlW6uCx36tFLjtDF7M9GjKi5Cd/jUFyjCypZnCyZkCmSjvL9iG/
Content-class: urn:content-classes:message
Subject: Test Design [SEC=UNCLASSIFIED:AUDIT]
MIME-Version: 1.0
Content-Type: multipart/alternative;
Bean Class Code
public class MailProcessor implements Processor {
public void process(Exchange exch) throws Exception {
String filename = exch.getIn().getHeader(Exchange.BREADCRUMB_ID,String.class);
POP3Message mailMSG = (POP3Message)exch.getIn(MailMessage.class).getMessage();
String mailTo = getRecipients(mailMSG);
String from = this.getFrom(mailMSG);`enter code here`
String subject = mailMSG.getSubject();
String headers = GetHeaders(mailMSG);
String body = this.getText(mailMSG);
Map<String, DataHandler> attachments = exch.getIn().getAttachments();
String attFileName = "";
if(attachments.size() > 0)
{
for(String name: attachments.keySet())
{
DataHandler dh = attachments.get(name);
attFileName = attFileName + "; " + dh.getName();
}
}
String strMessage = (" ----------- From:" + from + " ----------- To:" + mailTo
+ " ----------- Subject:" + subject + " ----------- Attchements:" + attFileName +
" ----------- Header:" + headers +" ----------- Body:" + body);
File mailFile = new File("D:\\IOI\\Mail\\" + filename + ".txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(mailFile));
writer.write(strMessage);
writer.close();
}
private String getText(Part p) throws
MessagingException, IOException
{
if (p.isMimeType("text/*")) {
String s = (String)p.getContent();
return s;
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart)p.getContent();
String text = null;
Part bp = null;
for (int i = 0; i < mp.getCount(); i++) {
bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return s;
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart)p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return s;
}
}
return null;
}
private String GetHeaders(Part p)
throws MessagingException
{
Enumeration<?> headers = p.getAllHeaders();
//Get the mail Header
String strHeader = "";
while(headers.hasMoreElements())
{
Header h = (Header)headers.nextElement();
strHeader = strHeader + "-" + h.getName() + " : " + h.getValue();
}
return strHeader;
}
private String getRecipients(Message msg)
throws MessagingException
{
javax.mail.Address[] addrs = msg.getRecipients(Message.RecipientType.TO);
if (addrs == null || addrs.length == 0)
return null;
String str = "";
for (int i = 0; i < addrs.length; i++) {
if (i != 0)
str += ", ";
String text = addrs[i].toString();
str += removeChars(text);
}
return str;
}
private String getFrom(Message msg)
throws MessagingException
{
javax.mail.Address[] addrs = msg.getFrom();
if (addrs == null || addrs.length == 0)
return null;
String str = "";
for (int i = 0; i < addrs.length; i++) {
if (i != 0)
str += ", ";
String text = addrs[i].toString();
str += removeChars(text);
}
return str;
}
private String removeChars(String text)
{
if (text == null)
return null;
StringBuffer buf = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&')
buf.append(' ');
else
buf.append(c);
}
return buf.toString();
}}
Thanks Sunil
exchange.getIn(MailMessage.class).getAllHeaders()
? – vikingsteve