Apache Camel:MailMessage电子邮件标题为空

时间:2014-01-16 00:01:46

标签: apache-camel

我是Apache骆驼的新手。我们使用Apache camel 2.11.0来轮询微软交换Indox,使用POP3作为FROM端点并创建一个bean类来处理路由中的电子邮件消息,然后再将其发送到目标端点。我遇到的问题是从处理器方法中的Exchange.getIn(MailMessage.class).getMessage()返回的MailMessage对象没有任何电子邮件标头。当我在FROM端点上放置一个Debug选项时,我可以在Apache驼峰跟踪中看到接收电子邮件的电子邮件标题。非常感谢您的帮助。

路线配置

<!-- Route email exchange-->
    <route id="gateMail">
        <from uri="pop3://MyUser@MyMailServer.com?password=MyPassword&amp;debugMode=true&amp;contentType=multipart/alternative" />
        <bean ref="MailProcessor" />            
        <to uri="file:D:/Mail" /></route>

在localhost [Apache Tomcat]的Tomcat v7.0服务器的控制台中输出c:\ Program Files \ Java \ jdk1.7.0_21 \ bin \ Javaw.exe

DEBUG POP3: pipeline message size 2243 S: +OK Received: by MyMailServer.com id <01CF1183.AA71B3C0@MyMailServer.com>; Wed, 15 Jan 2014 10:52:21 +1100 x-protective-marking: VER=2005.6, NS=com.au, SEC=UNCLASSIFIED:AUDIT, ORIGIN=MyUser@MyMailServer.com 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类代码

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

由于 苏尼尔

0 个答案:

没有答案