java - 如何接收电子邮件的附件?
问题描述
我想从收到的电子邮件(可能是其他一些文档文件)中保存附件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;
}
}
解决方案
解决了!
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;
}
推荐阅读
- javascript - 如何使用 nodeJS 使用 MySql 数据库中的数据创建 html 选择选项菜单
- android - SearchView + Filterable RecyclerView 使 SearchView 失去焦点
- c# - 选择游戏对象时如何弹出面板?
- sql - 多值查询
- oauth-2.0 - 如何在 JSONModel.loadData() 中使用 oAuth 身份验证
- r - 获取 2 个绘图和一个相邻的垂直图例
- c# - 如何将 XML 列转换为 SQL 数据库中的行?
- flutter - 每 x 分钟从数据库中检索数据以进行颤动
- for-loop - 如何在 gwbasic 中编写一个程序来将 1 到 100 的自然数相加?
- pycharm - Pycharm通过特定编码打开文件时,为什么是错误的编码?