我想从传入的电子邮件(可能是其他一些文档文件)中保存附件,例如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("[email protected]","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;
}
}
最佳答案
解决了!
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;
}
关于java - 如何从电子邮件接收附件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57375977/