Search in sources :

Example 11 with FileDataSource

use of javax.activation.FileDataSource in project orientdb by orientechnologies.

the class OMailPlugin method addAttachment.

/**
   * Adds a file as an attachment to the email's content
   *
   * @param multipart
   * @param filePath
   * @throws MessagingException
   */
private void addAttachment(final Multipart multipart, final String filePath) throws MessagingException {
    MimeBodyPart attachPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filePath);
    attachPart.setDataHandler(new DataHandler(source));
    attachPart.setFileName(new File(filePath).getName());
    multipart.addBodyPart(attachPart);
}
Also used : FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) File(java.io.File) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Example 12 with FileDataSource

use of javax.activation.FileDataSource in project spring-framework by spring-projects.

the class MimeMessageHelper method addInline.

/**
	 * Add an inline element to the MimeMessage, taking the content from a
	 * {@code java.io.File}.
	 * <p>The content type will be determined by the name of the given
	 * content file. Do not use this for temporary files with arbitrary
	 * filenames (possibly ending in ".tmp" or the like)!
	 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
	 * else, mail readers might not be able to resolve inline references correctly.
	 * @param contentId the content ID to use. Will end up as "Content-ID" header
	 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
	 * Can be referenced in HTML source via src="cid:myId" expressions.
	 * @param file the File resource to take the content from
	 * @throws MessagingException in case of errors
	 * @see #setText
	 * @see #addInline(String, org.springframework.core.io.Resource)
	 * @see #addInline(String, javax.activation.DataSource)
	 */
public void addInline(String contentId, File file) throws MessagingException {
    Assert.notNull(file, "File must not be null");
    FileDataSource dataSource = new FileDataSource(file);
    dataSource.setFileTypeMap(getFileTypeMap());
    addInline(contentId, dataSource);
}
Also used : FileDataSource(javax.activation.FileDataSource)

Example 13 with FileDataSource

use of javax.activation.FileDataSource in project spring-framework by spring-projects.

the class MimeMessageHelper method addAttachment.

/**
	 * Add an attachment to the MimeMessage, taking the content from a
	 * {@code java.io.File}.
	 * <p>The content type will be determined by the name of the given
	 * content file. Do not use this for temporary files with arbitrary
	 * filenames (possibly ending in ".tmp" or the like)!
	 * @param attachmentFilename the name of the attachment as it will
	 * appear in the mail
	 * @param file the File resource to take the content from
	 * @throws MessagingException in case of errors
	 * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
	 * @see #addAttachment(String, javax.activation.DataSource)
	 */
public void addAttachment(String attachmentFilename, File file) throws MessagingException {
    Assert.notNull(file, "File must not be null");
    FileDataSource dataSource = new FileDataSource(file);
    dataSource.setFileTypeMap(getFileTypeMap());
    addAttachment(attachmentFilename, dataSource);
}
Also used : FileDataSource(javax.activation.FileDataSource)

Example 14 with FileDataSource

use of javax.activation.FileDataSource in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method marshalAttachments.

@Test
public void marshalAttachments() throws Exception {
    marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(BinaryObject.class);
    marshaller.setMtomEnabled(true);
    marshaller.afterPropertiesSet();
    MimeContainer mimeContainer = mock(MimeContainer.class);
    Resource logo = new ClassPathResource("spring-ws.png", getClass());
    DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
    given(mimeContainer.convertToXopPackage()).willReturn(true);
    byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
    BinaryObject object = new BinaryObject(bytes, dataHandler);
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, new StreamResult(writer), mimeContainer);
    assertTrue("No XML written", writer.toString().length() > 0);
    verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) MimeContainer(org.springframework.oxm.mime.MimeContainer) Test(org.junit.Test)

Example 15 with FileDataSource

use of javax.activation.FileDataSource in project opennms by OpenNMS.

the class JavaMailer2 method createFileAttachment.

/*
    public Message buildMessage(String m_charSet, String m_encoding, String m_contentType) throws JavaMailerException {
        try {

            String encodedText = MimeUtility.encodeText(getMessageText(), m_charSet, m_encoding);
            if (getFileName() == null) {
                message.setContent(encodedText, m_contentType+"; charset="+m_charSet);
            } else {
                BodyPart bp = new MimeBodyPart();
                bp.setContent(encodedText, m_contentType+"; charset="+m_charSet);

                MimeMultipart mp = new MimeMultipart();
                mp.addBodyPart(bp);
                mp = new MimeMultipart();
                mp.addBodyPart(createFileAttachment(new File(getFileName())));
                message.setContent(mp);
            }

            message.setHeader("X-Mailer", getMailer());
            message.setSentDate(new Date());

            message.saveChanges();

            return message;
        } catch (AddressException e) {
            log().error("Java Mailer Addressing exception: ", e);
            throw new JavaMailerException("Java Mailer Addressing exception: ", e);
        } catch (MessagingException e) {
            log().error("Java Mailer messaging exception: ", e);
            throw new JavaMailerException("Java Mailer messaging exception: ", e);
        } catch (UnsupportedEncodingException e) {
            log().error("Java Mailer messaging exception: ", e);
            throw new JavaMailerException("Java Mailer encoding exception: ", e);
        }
    }
    */
/**
     * Create a file attachment as a MimeBodyPart, checking to see if the file
     * exists before we create the attachment.
     *
     * @param file file to attach
     * @return attachment body part
     * @throws javax.mail.MessagingException if we can't set the data handler or
     *      the file name on the MimeBodyPart
     * @throws org.opennms.javamail.JavaMailerException if the file does not exist or is not
     *      readable
     */
public MimeBodyPart createFileAttachment(final File file) throws MessagingException, JavaMailerException {
    if (!file.exists()) {
        LOG.error("File attachment '{}' does not exist.", file.getAbsolutePath());
        throw new JavaMailerException("File attachment '" + file.getAbsolutePath() + "' does not exist.");
    }
    if (!file.canRead()) {
        LOG.error("File attachment '{}' is not readable.", file.getAbsolutePath());
        throw new JavaMailerException("File attachment '" + file.getAbsolutePath() + "' is not readable.");
    }
    MimeBodyPart bodyPart = new MimeBodyPart();
    FileDataSource fds = new FileDataSource(file);
    bodyPart.setDataHandler(new DataHandler(fds));
    bodyPart.setFileName(fds.getName());
    return bodyPart;
}
Also used : FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

FileDataSource (javax.activation.FileDataSource)41 DataHandler (javax.activation.DataHandler)33 Exchange (org.apache.camel.Exchange)18 Message (org.apache.camel.Message)15 Test (org.junit.Test)15 File (java.io.File)12 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)12 MimeBodyPart (javax.mail.internet.MimeBodyPart)11 MimeMultipart (javax.mail.internet.MimeMultipart)7 Producer (org.apache.camel.Producer)7 DataSource (javax.activation.DataSource)6 BodyPart (javax.mail.BodyPart)6 Multipart (javax.mail.Multipart)6 Endpoint (org.apache.camel.Endpoint)6 Processor (org.apache.camel.Processor)6 MessagingException (javax.mail.MessagingException)5 InternetAddress (javax.mail.internet.InternetAddress)5 MimeMessage (javax.mail.internet.MimeMessage)5 List (java.util.List)4 DefaultAttachment (org.apache.camel.impl.DefaultAttachment)4