当前使用Javamail API在WSO2 ESB上工作。我想在这里做的是,下载未阅读的电子邮件(Gmail)附件到某些本地文件夹。 在ESB End中,阅读未读的电子邮件和获取未读的电子邮件的消息ID任务已经结束,并且还将该消息ID从ESB传递到Java。这是来自ESB端的。 我不知道如何下载Java中该特定消息ID的电子邮件附件。
您能帮我找到解决方案吗? 预先感谢,
按下代码,将所有电子邮件附件下载到某些本地文件夹中。
public void downloadEmailAttachments(String host, String port,
String userName, String password) {
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", port);
properties.setProperty("mail.pop3.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
properties.setProperty("mail.pop3.socketFactory.port",
String.valueOf(port));
Session session = Session.getDefaultInstance(properties);
try {
Store store = session.getStore("pop3");
store.connect(userName, password);
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);
Message[] arrayMessages = folderInbox.getMessages();
for (int i = 0; i < arrayMessages.length; i++) {
Message message = arrayMessages[i];
Address[] fromAddress = message.getFrom();
String from = fromAddress[0].toString();
String subject = message.getSubject();
String sentDate = message.getSentDate().toString();
String contentType = message.getContentType();
String messageContent = "";
// store attachment file name, separated by comma
String attachFiles = "";
if (contentType.contains("multipart")) {
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(saveDirectory + File.separator + fileName);
} else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain")
|| contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}
// print out details of each message
System.out.println("Message #" + (i + 1) + ":");
System.out.println("t From: " + from);
System.out.println("t Subject: " + subject);
System.out.println("t Sent Date: " + sentDate);
System.out.println("t Message: " + messageContent);
System.out.println("t Attachments: " + attachFiles);
}
// disconnect
folderInbox.close(false);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println("No provider for pop3.");
ex.printStackTrace();
} catch (MessagingException ex) {
System.out.println("Could not connect to the message store");
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
使用POP3没有有效的方法来执行此操作。POP3将要求客户端至少读取每个消息的所有标题,以便找到与MessageID匹配的消息。IMAP可以更有效地做到这一点。
使用任何一个协议,您都可以使用folder.search方法与messageIdterm查找正确的消息。
还请注意,您需要在代码中修复这些常见的Javamail错误。
假设您已经有消息ID,这很简单。您在文件夹对象上具有getMessage()函数,您可以将其用于单个消息
Message message = folderInbox.getMessage(messageId);
这应该有效。
***我建议您通过API