Example usage for javax.mail.internet MimePart setFileName

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

Introduction

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

Prototype

public void setFileName(String filename) throws MessagingException;

Source Link

Document

Set the filename associated with this part, if possible.

Usage

From source file:mitm.common.pdf.MessagePDFBuilder.java

private Part convertRFC822(Part attachment) {
    try {/* w  ww  . j  av a2s  .  c  o  m*/
        Object o = attachment.getContent();

        if (o instanceof MimeMessage) {
            MimeMessage message = (MimeMessage) o;

            MessagePDFBuilder pdfBuilder = this.clone();

            ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();

            pdfBuilder.buildPDF(message, null, pdfStream);

            MimePart pdfPart = new MimeBodyPart();

            pdfPart.setDataHandler(
                    new DataHandler(new ByteArrayDataSource(pdfStream.toByteArray(), "application/pdf")));

            String filename = getFilename(attachment, "message.pdf");

            if (!filename.toLowerCase().endsWith(".pdf")) {
                filename = filename + ".pdf";
            }

            pdfPart.setFileName(filename);

            return pdfPart;
        }
    } catch (IOException e) {
        logger.error("Error trying to converting to RFC822.");
    } catch (MessagingException e) {
        logger.error("Error trying to converting to RFC822.");
    } catch (DocumentException e) {
        logger.error("Error trying to converting to RFC822.");
    }

    return attachment;
}