【问题标题】:how to read mails with attached from MS exchange with java mail and EWS如何使用 java 邮件和 EWS 从 MS 交换中读取附加的邮件
【发布时间】:2013-12-28 03:54:06
【问题描述】:

当我使用 javamail4ews-0.0.8 从 MS Exchange Server 2010 读取邮件时遇到问题 我连接成功 我阅读了我拥有的每封邮件,但是当我尝试查看附件时,我什么也看不到。 我看 contentType 他的 valor 是“text/plain”,当我读到 getContent().getClass() 是 String。如果我看到包含文本/纯文本的信息,这是一个 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);
    }
}
  }

【问题讨论】:

    标签: java email jakarta-mail


    【解决方案1】:

    您需要了解有关 Internet 电子邮件和 MIME 消息的更多信息。从这些 JavaMail 常见问题条目开始:

    具体来说,请参阅上面链接中提供的 msgshow.java 演示程序。

    【讨论】:

      【解决方案2】:

      使用 JWebServices for Exchange 看起来很简单

      Find messages with attachments

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-24
        • 1970-01-01
        • 2017-02-18
        • 2017-11-18
        • 2017-03-15
        • 1970-01-01
        • 2017-09-21
        • 1970-01-01
        相关资源
        最近更新 更多