gpt4 book ai didi

java - 如何从电子邮件接收附件?

转载 作者:行者123 更新时间:2023-12-02 02:00:47 26 4
gpt4 key购买 nike

我想保存传入电子邮件中的附件,例如 docdocx(也许是其他一些文档文件)。我写了一个方法,但它只在磁盘上创建相应名称和扩展名的文件,但不向其中写入内容。我使用 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;
}

关于java - 如何从电子邮件接收附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57375977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com