spring - How can attachment names be retrieved from a JavaMailSender exception? -
i'm using org.springframework.mail.javamail.javamailsender (spring framework 4.1.6). i'm sending multiple emails calling:
mailsender.send(mimemessagepreparators);
where mimemessagepreparators mimemessagepreparator array. each mimemessagepreparator built follows:
mimemessagepreparator mimemessagepreparator = new mimemessagepreparator() { public void prepare(mimemessage mimemessage) throws messagingexception { mimemessagehelper mimemessagehelper = new mimemessagehelper(mimemessage, true); // subscribers of attachment , put them recipients // of email mimemessagehelper.setto(subscribers); // email have same from, bcc, reply to, subject, , body string fromemailaddress = emailtemplate.getfromemailaddress(); mimemessagehelper.setfrom(fromemailaddress); // note: bcc sender email mimemessagehelper.setbcc(fromemailaddress); // on auto replies , bounce messages // should on deliverability mimemessagehelper.setreplyto(fromemailaddress); string subject = emailtemplate.getsubject(); mimemessagehelper.setsubject(subject); string emailbody = emailtemplate.getbody(); mimemessagehelper.settext(open_email_tags + emailbody + close_email_tags, true); // physical file , add email attachment filesystemresource file = new filesystemresource(new file(directory, attachment.getname())); mimemessagehelper.addattachment(attachment.getname(), file); } };
i need know emails failed (i.e. had mailexception) , tell user names of attachments associated emails failed. how can retrieve attachment names exception? far, have
try { mailsender.send(mimemessagepreparators); } catch (mailsendexception mailsendexception) { map<object, exception> map = mailsendexception.getfailedmessages(); (map.entry<object, exception> entry : map.entryset()) { mimemessage mimemessage = (mimemessage) entry.getkey(); // attachment names mimemessage? or preferably // in more simplistic way using helper such mimemessagehelper } catch (mailexception mailexception) { // how attachment names here? }
if have bunch of mimemessage objects, see javamail faq entries starting here:
essentially, need iterate on parts in message, determine ones represent attachments, , access whatever metadata or headers in part think represent attachment "name".