Search in sources :

Example 66 with DataSource

use of javax.activation.DataSource in project jbpm-work-items by kiegroup.

the class SendMailWorkitemHandler method createEmailWithAttachment.

public MimeMessage createEmailWithAttachment(String to, String from, String subject, String bodyText, Document attachment) throws MessagingException, IOException {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    MimeMessage email = new MimeMessage(session);
    email.setFrom(new InternetAddress(from));
    email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
    email.setSubject(subject);
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setContent(bodyText, "text/plain");
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(mimeBodyPart);
    if (attachment != null) {
        mimeBodyPart = new MimeBodyPart();
        DataSource source = new InputStreamDataSource(new ByteArrayInputStream(attachment.getContent()));
        mimeBodyPart.setDataHandler(new DataHandler(source));
        mimeBodyPart.setFileName(attachment.getName());
        multipart.addBodyPart(mimeBodyPart);
    }
    email.setContent(multipart);
    return email;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayInputStream(java.io.ByteArrayInputStream) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session) DataSource(javax.activation.DataSource)

Example 67 with DataSource

use of javax.activation.DataSource in project simple-java-mail by bbottema.

the class NamedDataSourceTest method contentTypeStreamWillBeTheSame1.

@Test
public void contentTypeStreamWillBeTheSame1() throws Exception {
    DataSource testDataSource = new NamedDataSource("newName", dataSource);
    testDataSource.getContentType();
    verify(dataSource).getContentType();
}
Also used : DataSource(javax.activation.DataSource) Test(org.junit.Test)

Example 68 with DataSource

use of javax.activation.DataSource in project simple-java-mail by bbottema.

the class NamedDataSourceTest method renameWillWork.

@Test
public void renameWillWork() throws Exception {
    DataSource testDataSource = new NamedDataSource("newName", dataSource);
    assertThat(testDataSource.getName()).isEqualTo("newName");
    verifyZeroInteractions(dataSource);
}
Also used : DataSource(javax.activation.DataSource) Test(org.junit.Test)

Example 69 with DataSource

use of javax.activation.DataSource in project simple-java-mail by bbottema.

the class NamedDataSourceTest method outputStreamWillBeTheSame1.

@Test
public void outputStreamWillBeTheSame1() throws Exception {
    DataSource testDataSource = new NamedDataSource("newName", dataSource);
    testDataSource.getOutputStream();
    verify(dataSource).getOutputStream();
}
Also used : DataSource(javax.activation.DataSource) Test(org.junit.Test)

Example 70 with DataSource

use of javax.activation.DataSource in project simple-java-mail by bbottema.

the class MimeMessageParser method createDataSource.

/**
 * Parses the MimePart to create a DataSource.
 *
 * @param part the current part to be processed
 * @return the DataSource
 */
@Nonnull
private static DataSource createDataSource(@Nonnull final MimePart part) {
    final DataHandler dataHandler = retrieveDataHandler(part);
    final DataSource dataSource = dataHandler.getDataSource();
    final String contentType = parseBaseMimeType(dataSource.getContentType());
    final byte[] content = readContent(retrieveInputStream(dataSource));
    final ByteArrayDataSource result = new ByteArrayDataSource(content, contentType);
    final String dataSourceName = parseDataSourceName(part, dataSource);
    result.setName(dataSourceName);
    return result;
}
Also used : DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) Nonnull(javax.annotation.Nonnull)

Aggregations

DataSource (javax.activation.DataSource)196 DataHandler (javax.activation.DataHandler)114 FileDataSource (javax.activation.FileDataSource)60 MimeBodyPart (javax.mail.internet.MimeBodyPart)53 MimeMultipart (javax.mail.internet.MimeMultipart)48 MimeMessage (javax.mail.internet.MimeMessage)39 InputStream (java.io.InputStream)37 IOException (java.io.IOException)36 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)35 ByteArrayInputStream (java.io.ByteArrayInputStream)33 File (java.io.File)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)30 InternetAddress (javax.mail.internet.InternetAddress)27 Properties (java.util.Properties)26 MessagingException (javax.mail.MessagingException)25 Multipart (javax.mail.Multipart)24 Session (javax.mail.Session)21 BodyPart (javax.mail.BodyPart)19 ArrayList (java.util.ArrayList)16