Example usage for javax.mail.internet MimeMessage setFileName

List of usage examples for javax.mail.internet MimeMessage setFileName

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage setFileName.

Prototype

@Override
public void setFileName(String filename) throws MessagingException 

Source Link

Document

Set the filename associated with this part, if possible.

Usage

From source file:mitm.application.djigzo.james.mailets.Attach.java

@Override
public void serviceMail(Mail mail) {
    try {/*from  w  w  w  . j av a  2s .  c  o  m*/
        MimeMessage sourceMessage = mail.getMessage();

        MimeMessage newMessage = retainMessageID
                ? new MimeMessageWithID(MailSession.getDefaultSession(), sourceMessage.getMessageID())
                : new MimeMessage(MailSession.getDefaultSession());

        if (StringUtils.isNotEmpty(filename)) {
            newMessage.setFileName(filename);
        }

        Multipart mp = new MimeMultipart();

        HeaderMatcher contentMatcher = new ContentHeaderNameMatcher();

        mp.addBodyPart(BodyPartUtils.makeContentBodyPart(sourceMessage, contentMatcher));

        newMessage.setContent(mp);

        /* 
         * create a matcher that matches on everything expect content-* 
         */
        HeaderMatcher nonContentMatcher = new NotHeaderNameMatcher(contentMatcher);

        /* 
         * copy all non-content headers from source message to the new message 
         */
        HeaderUtils.copyHeaders(sourceMessage, newMessage, nonContentMatcher);

        newMessage.saveChanges();

        mail.setMessage(newMessage);
    } catch (MessagingException e) {
        getLogger().error("Error attaching the message.", e);
    } catch (IOException e) {
        getLogger().error("Error attaching the message.", e);
    }
}