如何阅读与Java邮件和EWS的MS交换附加的邮件

时间:2013-12-10 14:58:45

标签: java email javamail

当我使用javamail4ews-0.0.8从MS Exchange Server 2010读取邮件时,我遇到了问题 我成功连接 我读了每封邮件,但是当我试着看到附件时,我什么都看不到。 我查看contentType,他的valor是“text / plain”,当我读取getContent()时,getClass()是String。如果我看到包含text / plain的信息,那么这是一个MimeMultipart,因为它有leyend MIME-Version:1.0

我查看邮件并包含PDF和XML

我不知道为什么程序会给我一个contentType =“text / plain”和getContent()。getClass()= String

我需要一个Multipart或MimeMultipart,但我不知道如何转换它。

我添加了我的代码......

谢谢你们。

import javax.mail.MessagingException;
import javax.mail.Store;
public class Test {

 public static void main(String[] args){
 StoreMail test = new StoreMail(
         "user",
         "password",
         "https://<exchangeServer>/ews/exchange.asmx",
         "ewsstore",
         "C:\\");
 //public StoreMail( String userMail, String passMail, String host, String protocolo, String directorio) {
 try {
    test.readEmails(1);
 } catch (MessagingException e) {
    e.printStackTrace();
 }
 }
}

public class StoreMail {    
public StoreMail( String userMail, String passMail, String host, String protocolo, String directorio) {
    ....
}
public void readEmails( int cantidad ) throws MessagingException {  
    Properties props = System.getProperties();
    props.setProperty("directorio","C:\\BASURA\\\\" );
    props.setProperty("dircliente","C:\\BASURA\\\\" );
    Session session = Session.getInstance(props, null);

    System.out.println( host+" "+userMail+" "+passMail );
    Store store = session.getStore(protocolo);
    store.connect(host, userMail, passMail);

    Folder currFolder = store.getDefaultFolder();
    Message[] messages = currFolder.getMessages();
    for (Message message : messages) {
        try {
                Object body = message.getContent();
                System.out.println("BODY:::::"+body.getClass()); // is class java.lang.String
                if (body instanceof Multipart) { // or instanceof MimeMultipart
                    System.out.println( " MimeMultipart" );
                    procMultipart((Multipart) body)
                } else {
                    System.out.println(" other:");
                    procPart(message);
                }   


        }catch (Throwable thrbl) {
                thrbl.printStackTrace();
        }

    }

}
public void procPart( Part p ) {
    try {
        String contentType = p.getContentType();

        if ( contentType != null ){
            //if (p.isMimeType("multipart/alternative")) {
                //System.out.println("is multipart/alternative");
            //}else{
                if ( contentType.toLowerCase().startsWith( "multipart/" ) ) {
                    procMultipart(( Multipart ) p)
                }else{
                    if(contentType.toLowerCase().startsWith( "text/plain" )){
                        //I don't know what to do because is a Mime Multipart... and I need to read it
                        // I need transform the attached to PDF and XML and Save it
                    }else{
                        System.out.println("other contentType"+contentType);
                        String nomfich = p.getFileName();
                        if (nomfich != null) {
                            if( nomfich.toLowerCase().endsWith( ".xml" ) ) {
                                System.out.println(" XML octet-stream nomfich: " + nomfich);
                                saveArchXML( p.getInputStream() );

                            } else if( nomfich.toLowerCase().endsWith( ".pdf" ) ) {
                                saveArchPDF( p.getInputStream(),nomfich.toLowerCase() );
                            } else if( nomfich.toLowerCase().endsWith( ".zip" ) ) {
                                saveArchZIP( p.getInputStream(),nomfich.toLowerCase() );
                            }else{

                            }
                        }
                    }
                }
            //}
        }
    } catch ( Exception ex ) {
        ex.printStackTrace();
    }

}

public void procMultipart(Multipart mp) throws MessagingException {
    for (int i = 0; i < mp.getCount(); i++) {
        logger.info(" procMultipart :" + i);
        Part p = mp.getBodyPart(i);
        procPart(p);
    }
}
  }

2 个答案:

答案 0 :(得分:0)

您需要了解有关互联网电子邮件和MIME邮件的更多信息。从这些JavaMail FAQ条目开始:

特别是,请参阅上面链接中提供的msgshow.java演示程序。

答案 1 :(得分:0)

使用JWebServices for Exchange,它看起来很简单

Find messages with attachments