首页 > 解决方案 > 如何接收电子邮件的附件?

问题描述

我想从收到的电子邮件(可能是其他一些文档文件)中保存附件doc,例如。docx我写了一个方法,但它只在磁盘上创建相应名称和扩展名的文件,而不向它们写入内容。我使用ews java api连接到服务器。我认为问题在于该方法没有看到文件的来源以便通过流写入它。也许还有另一种工作方式?请帮我纠正:

public class ReadMail {

    private String from;
    private String subject;
    private String date;
    public ObservableList<ReadMail> mailList = FXCollections.observableArrayList();
    public FindItemsResults<Item> findResults;
    public ExchangeService service;
    public ReadMail() throws Exception {

        service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        ExchangeCredentials credentials = new WebCredentials("mail@email.local","pass");
        service.setCredentials(credentials);
        service.setUrl(new URI("https://server-exch.email.local/EWS/Exchange.asmx"));

        ItemView view = new ItemView (3);
        findResults = (FindItemsResults<Item>)service.findItems(WellKnownFolderName.Inbox, view);

        for(Item item : findResults.getItems()){
          item.load(new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent));
          System.out.println("From: " + item.getLastModifiedName());
          System.out.println("Subject: " + item.getSubject());
          System.out.println("Date: " + item.getDateTimeReceived());
    }

    public ReadMail(String  from, String  subject, String date) {
        this.from = from;
        this.subject = subject;
        this.date = date;
    }

    public ObservableList<ReadMail> mailList() throws Exception{
        SimpleDateFormat formatter = new SimpleDateFormat("EE dd.MM.yyyy HH:mm");
        for (Item item : findResults.getItems()) {
            EmailMessage message = EmailMessage.bind(service, item.getId());
            mailList.add(new ReadMail(message.getSender().getName(), item.getSubject(), formatter.format(message.getDateTimeReceived())));
            AttachmentCollection attachmentsCollection = message.getAttachments();
            for (int i = 0; i < attachmentsCollection.getCount(); i++) {
               Attachment attachment = attachmentsCollection.getPropertyAtIndex(i);
               FileOutputStream fileOutputStream = new FileOutputStream("D:\\test\\" + attachment.getName() , false);
                byte[] buffer = attachment.getName().getBytes();
                fileOutputStream.write(buffer, 0, buffer.length);
                fileOutputStream.close();
                fileOutputStream.close();
            }                       
        }
        return mailList;
    }
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
    public String fromProperty() {
        return from;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String subjectProperty() {
        return subject;
    }
    public String getdate() {
        return date;
    }
    public void setdate(String date) {
        this.date = date;
    }
    public String dateProperty() {
        return date;
    }
}

标签: javaemailexchangewebservicesattachment

解决方案


解决了!

public ObservableList<ReadMail> mailList() throws Exception{

        String[] attachExtensions = {".doc", ".docx"};
        SimpleDateFormat formatter = new SimpleDateFormat("EE dd.MM.yyyy HH:mm");

        for (Item item : findResults.getItems()) {

            EmailMessage message = EmailMessage.bind(service, item.getId());
            mailList.add(new ReadMail(message.getSender().getName(), item.getSubject(), formatter.format(message.getDateTimeReceived())));
            AttachmentCollection attachmentsCollection = message.getAttachments();

            for (int i = 0; i < attachmentsCollection.getCount(); i++) {

                FileAttachment fileattaAttachment = (FileAttachment) attachmentsCollection.getPropertyAtIndex(i);

                for (int j = 0; j < attachExtensions.length; j++) {
                    if (fileattaAttachment.getName().contains(attachExtensions[i])) {
                        fileattaAttachment.load("D:\\test\\" + fileattaAttachment.getName());
                    }
                }
                System.out.println("Type: " + fileattaAttachment.getContentType()); 
            }                   
        }
        return mailList;
    }

推荐阅读