Pobierz załączniki za pomocą poczty Java
Teraz, gdy pobrałem wszystkie wiadomości i zapisałem je do
Message[] temp;
Jak uzyskać listę załączników dla każdej z tych wiadomości do
List<File> attachments;
Uwaga: żadnych bibliotek stron trzecich, proszę, tylko JavaMail.
5 answers
Bez obsługi wyjątków, ale tutaj:
List<File> attachments = new ArrayList<File>();
for (Message message : temp) {
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&
StringUtils.isBlank(bodyPart.getFileName())) {
continue; // dealing with attachments only
}
InputStream is = bodyPart.getInputStream();
File f = new File("/tmp/" + bodyPart.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while((bytesRead = is.read(buf))!=-1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
attachments.add(f);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-08 07:34:54
Pytanie jest bardzo stare, ale może komuś pomoże. Chciałbym poszerzyć odpowiedź Davida Rabinowitza.
if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()))
Nie powinien zwracać wszystkich atachments, jak się tego spodziewasz, ponieważ możesz mieć pocztę, w której mieszana część jest bez zdefiniowanego usposobienia.
----boundary_328630_1e15ac03-e817-4763-af99-d4b23cfdb600
Content-Type: application/octet-stream;
name="00000000009661222736_236225959_20130731-7.txt"
Content-Transfer-Encoding: base64
Więc w tym przypadku możesz również sprawdzić nazwę pliku. Tak:
if (!Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) && StringUtils.isBlank(part.getFileName())) {...}
EDIT
Istnieje cały działający kod używając warunku opisanego powyżej.. Ponieważ każda część może hermetyzować Inne części i attachment powinien być zagnieżdżony, rekurencja jest używana do przechodzenia przez wszystkie części
public List<InputStream> getAttachments(Message message) throws Exception {
Object content = message.getContent();
if (content instanceof String)
return null;
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
List<InputStream> result = new ArrayList<InputStream>();
for (int i = 0; i < multipart.getCount(); i++) {
result.addAll(getAttachments(multipart.getBodyPart(i)));
}
return result;
}
return null;
}
private List<InputStream> getAttachments(BodyPart part) throws Exception {
List<InputStream> result = new ArrayList<InputStream>();
Object content = part.getContent();
if (content instanceof InputStream || content instanceof String) {
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || StringUtils.isNotBlank(part.getFileName())) {
result.add(part.getInputStream());
return result;
} else {
return new ArrayList<InputStream>();
}
}
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
result.addAll(getAttachments(bodyPart));
}
}
return result;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-01 20:57:08
Oszczędność czasu dla kodu, w którym zapisujesz plik załącznika:
W wersji javax mail 1.4 a potem możesz powiedzieć
bodyPart.saveFile("/tmp/" + bodyPart.getFileName());
Zamiast
InputStream is = bodyPart.getInputStream();
File f = new File("/tmp/" + bodyPart.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while((bytesRead = is.read(buf))!=-1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-04 14:16:08
Możesz po prostu użyć Apache Commons Mail API MimeMessageParser-getAttachmentList () wraz z Commons IO i Commons Lang.
MimeMessageParser parser = ....
parser.parse();
for(DataSource dataSource : parser.getAttachmentList()) {
if (StringUtils.isNotBlank(dataSource.getName())) {}
//use apache commons IOUtils to save attachments
IOUtils.copy(dataSource.getInputStream(), ..dataSource.getName()...)
} else {
//handle how you would want attachments without file names
//ex. mails within emails have no file name
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-02 19:57:12
Oto moje wykonanie rozwiązania mefi .
private static void attachments(
final BodyPart body, final BiConsumer<String, InputStream> consumer)
throws MessagingException, IOException {
final Multipart content;
try {
content = (Multipart) body.getContent();
for (int i = 0; i < content.getCount(); i++) {
attachments(content.getBodyPart(i), consumer);
}
return;
} catch (final ClassCastException cce) {
}
if (!Part.ATTACHMENT.equalsIgnoreCase(body.getDisposition())) {
return;
}
final String name = body.getFileName();
if (name == null || name.trim().isEmpty()) {
return;
}
try (final InputStream stream = body.getInputStream()) {
consumer.accept(name, stream);
}
}
public static void attachments(
final Message message, final BiConsumer<String, InputStream> consumer)
throws IOException, MessagingException {
final Multipart content;
try {
content = (Multipart) message.getContent();
} catch (final ClassCastException cce) {
return;
}
for (int i = 0; i < content.getCount(); i++) {
attachments(content.getBodyPart(i), consumer);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:07