1
votes

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&amp;debugMode=true&amp;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

1
Did you try exchange.getIn(MailMessage.class).getAllHeaders()?vikingsteve
Thanks for your quick response, I can't find getAllHeaders() method in exchange.getIn(MailMessage.class) so i have used getHeaders() and watch output shows only one header {breadcrumbId=ID-MyServer-2394-1389906921689-0-1} . I have updated the post with some more information that might help you to debug the issue.user3200426

1 Answers

0
votes

I had this same issue and the cause a was conflict on library javax.mail, which is a transitive dependency of camel-mail and also of another library I had on my project.
To fix this, you'll have to exclude the conflicting dependency.

Finding the conflicting dependency with Maven

Run the following command and check if javax.mail is listed as a transitive dependency of another dependency.
Please be aware that other dependencies may import the same classes that exist on javax.mail, but on a different dependence, and this can cause the same issue.
On my case this happened with org.apache.geronimo.specs:geronimo-javamail_1.4_spec

mvn dependency:tree -Dverbose

Output snippet:

[INFO] +- org.apache.neethi:neethi:jar:2.0.4:compile
[INFO] |  +- org.apache.ws.commons.axiom:axiom-api:jar:1.2.7:compile
[INFO] |  |  +- org.apache.geronimo.specs:geronimo-javamail_1.4_spec:jar:1.2:compile

Excluding the dependency with Maven

Now that you've found which dependency imports the conflicting transitive dependency, add the exclusion to your pom.

    <dependency>
      <groupId>org.apache.neethi</groupId>
      <artifactId>neethi</artifactId>
      <version>${version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.geronimo.specs</groupId>
          <artifactId>geronimo-javamail_1.4_spec</artifactId>
        </exclusion>
      </exclusions>
    </dependency>