Search in sources :

Example 21 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project simple-java-mail by bbottema.

the class EmailPopulatingBuilderImpl method withEmbeddedImage.

/**
 * @see EmailPopulatingBuilder#withEmbeddedImage(String, byte[], String)
 */
@Override
public EmailPopulatingBuilder withEmbeddedImage(@NotNull final String name, @NotNull final byte[] data, @NotNull final String mimetype) {
    checkNonEmptyArgument(name, "name");
    checkNonEmptyArgument(data, "data");
    checkNonEmptyArgument(mimetype, "mimetype");
    final ByteArrayDataSource dataSource = new ByteArrayDataSource(data, mimetype);
    dataSource.setName(name);
    return withEmbeddedImage(name, dataSource);
}
Also used : ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Example 22 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project simple-java-mail by bbottema.

the class EmailPopulatingBuilderImpl method withAttachment.

/**
 * @see EmailPopulatingBuilder#withAttachment(String, byte[], String)
 */
@Override
public EmailPopulatingBuilder withAttachment(@Nullable final String name, @NotNull final byte[] data, @NotNull final String mimetype) {
    checkNotNull(data, "data");
    checkNonEmptyArgument(mimetype, "mimetype");
    final ByteArrayDataSource dataSource = new ByteArrayDataSource(data, mimetype);
    dataSource.setName(name);
    withAttachment(name, dataSource);
    return this;
}
Also used : ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Example 23 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project simple-java-mail by bbottema.

the class EmailHelper method createDummyEmailBuilder.

public static EmailPopulatingBuilder createDummyEmailBuilder(@Nullable String id, boolean includeSubjectAndBody, boolean onlyBasicFields, boolean includeCustomHeaders, boolean useSmimeDetailsImplFromSmimeModule, final boolean fixSentDate, final boolean useDynamicImageEmbedding, final boolean includeCalendarText) throws IOException {
    EmailPopulatingBuilder builder = EmailBuilder.startingBlank().fixingMessageId(id).from("lollypop", "lol.pop@somemail.com").to("C.Cane", "candycane@candyshop.org");
    if (!onlyBasicFields) {
        // normally not needed, but for the test it is because the MimeMessage will
        // have it added automatically as well, so the parsed Email will also have it then
        builder = builder.withReplyTo("lollypop-reply", "lol.pop.reply@somemail.com").withBounceTo("lollypop-bounce", "lol.pop.bounce@somemail.com");
    }
    if (includeSubjectAndBody) {
        builder = builder.withSubject("hey").withPlainText("We should meet up!").withHTMLText("<b>We should meet up!</b><img src='cid:thumbsup'>");
    }
    if (useDynamicImageEmbedding) {
        builder = builder.appendTextHTML("<img src=\"/test-dynamicembedded-image/br2049.jpg\"/>");
    }
    if (includeCustomHeaders) {
        builder = builder.withHeader("dummyHeader", "dummyHeaderValue").withHeader("dummyHeader", "dummyHeaderValueSecond").withHeader("anotherDummyHeader", "anotherDummyHeaderValue").withDispositionNotificationTo("simple@address.com").withReturnReceiptTo("Complex Email", "simple@address.com");
    }
    if (includeCalendarText) {
        builder = builder.withCalendarText(CalendarMethod.CANCEL, "Sorry, can't make it");
    }
    if (fixSentDate) {
        builder = builder.fixingSentDate(CUSTOM_SENT_DATE);
    }
    // add two text files in different ways and a black thumbs up embedded image ->
    ByteArrayDataSource namedAttachment = new ByteArrayDataSource("Black Tie Optional", "text/plain");
    namedAttachment.setName("dresscode-ignored-because-of-override.txt");
    String base64String = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABeElEQVRYw2NgoAAYGxu3GxkZ7TY1NZVloDcAWq4MxH+B+D8Qv3FwcOCgtwM6oJaDMTAUXOhmuYqKCjvQ0pdoDrCnmwNMTEwakC0H4u8GBgYC9Ap6DSD+iewAoIPm0ctyLqBlp9F8/x+YE4zpYT8T0LL16JYD8U26+B7oyz4sloPwenpYno3DchCeROsUbwa05A8eB3wB4kqgIxOAuArIng7EW4H4EhC/B+JXQLwDaI4ryZaDSjeg5mt4LCcFXyIn1fdSyXJQVt1OtMWGhoai0OD8T0W8GohZifE1PxD/o7LlsPLiFNAKRrwOABWptLAcqc6QGDAHQEOAYaAc8BNotsJAOgAUAosG1AFA/AtUoY3YEFhKMAvS2AE7iC1+WaG1H6gY3gzE36hUFJ8mqzbU1dUVBBqQBzTgIDQRkWo5qCZdpaenJ0Zx1aytrc0DDB0foIG1oAYKqC0IZK8D4n1AfA6IzwPxXpCFoGoZVEUDaRGGUTAKRgEeAAA2eGJC+ETCiAAAAABJRU5ErkJggg==";
    InternalEmailPopulatingBuilder internalBuilder = ((InternalEmailPopulatingBuilder) builder.withAttachment("dresscode.txt", namedAttachment).withAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain").withEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png")).withDecryptedAttachments(builder.getAttachments());
    if (useSmimeDetailsImplFromSmimeModule) {
        internalBuilder.withOriginalSmimeDetails(OriginalSmimeDetailsImpl.builder().build());
    }
    return internalBuilder;
}
Also used : EmailPopulatingBuilder(org.simplejavamail.api.email.EmailPopulatingBuilder) InternalEmailPopulatingBuilder(org.simplejavamail.email.internal.InternalEmailPopulatingBuilder) InternalEmailPopulatingBuilder(org.simplejavamail.email.internal.InternalEmailPopulatingBuilder) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Example 24 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource 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
 */
@NotNull
private static DataSource createDataSource(@NotNull final MimePart part, final boolean fetchAttachmentData) {
    final DataSource dataSource = retrieveDataHandler(part).getDataSource();
    final String dataSourceName = parseDataSourceName(part, dataSource);
    if (fetchAttachmentData) {
        final String contentType = MiscUtil.parseBaseMimeType(dataSource.getContentType());
        final ByteArrayDataSource result = new ByteArrayDataSource(readContent(retrieveInputStream(dataSource)), contentType);
        result.setName(dataSourceName);
        return result;
    } else {
        return new NamedDataSource(dataSourceName, dataSource);
    }
}
Also used : ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) NamedDataSource(org.simplejavamail.internal.util.NamedDataSource) DataSource(jakarta.activation.DataSource) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) NamedDataSource(org.simplejavamail.internal.util.NamedDataSource) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project simple-java-mail by bbottema.

the class OutlookEmailConverter method buildEmailFromOutlookMessage.

private static EmailFromOutlookMessage buildEmailFromOutlookMessage(@NotNull final EmailPopulatingBuilder builder, @NotNull final OutlookMessage outlookMessage, @NotNull final EmailPopulatingBuilderFactory builderFactory, @NotNull final InternalEmailConverter internalEmailConverter) {
    checkNonEmptyArgument(builder, "emailBuilder");
    checkNonEmptyArgument(outlookMessage, "outlookMessage");
    String fromEmail = ofNullable(outlookMessage.getFromEmail()).orElse("donotreply@unknown-from-address.net");
    builder.from(outlookMessage.getFromName(), fromEmail);
    builder.fixingMessageId(outlookMessage.getMessageId());
    // FIXME creation date?
    builder.fixingSentDate(ofNullable(outlookMessage.getClientSubmitTime()).orElse(outlookMessage.getDate()));
    if (!MiscUtil.valueNullOrEmpty(outlookMessage.getReplyToEmail())) {
        builder.withReplyTo(outlookMessage.getReplyToName(), outlookMessage.getReplyToEmail());
    }
    copyReceiversFromOutlookMessage(builder, outlookMessage);
    builder.withSubject(outlookMessage.getSubject());
    builder.withPlainText(outlookMessage.getBodyText());
    builder.withHTMLText(outlookMessage.getBodyHTML() != null ? outlookMessage.getBodyHTML() : outlookMessage.getConvertedBodyHTML());
    for (final Map.Entry<String, OutlookFileAttachment> cid : outlookMessage.fetchCIDMap().entrySet()) {
        final String cidName = checkNonEmptyArgument(cid.getKey(), "cid.key");
        builder.withEmbeddedImage(verifyNonnullOrEmpty(extractCID(cidName)), cid.getValue().getData(), cid.getValue().getMimeTag());
    }
    for (final OutlookFileAttachment attachment : outlookMessage.fetchTrueAttachments()) {
        String attachmentName = ofNullable(attachment.getLongFilename()).orElse(attachment.getFilename());
        builder.withAttachment(attachmentName, attachment.getData(), attachment.getMimeTag());
    }
    for (int i = 0; i < outlookMessage.getOutlookAttachments().size(); i++) {
        final OutlookAttachment attachment = outlookMessage.getOutlookAttachments().get(i);
        if (attachment instanceof OutlookMsgAttachment) {
            final OutlookMessage nestedMsg = ((OutlookMsgAttachment) attachment).getOutlookMessage();
            final Email email = buildEmailFromOutlookMessage(builderFactory.create(), nestedMsg, builderFactory, internalEmailConverter).getEmailBuilder().buildEmail();
            final MimeMessage message = internalEmailConverter.emailToMimeMessage(email);
            final byte[] mimedata = internalEmailConverter.mimeMessageToEMLByteArray(message);
            builder.withAttachment(nestedMsg.getSubject() + ".eml", new ByteArrayDataSource(mimedata, "message/rfc822"));
        }
    }
    return new EmailFromOutlookMessage(builder, new OutlookMessageProxy(outlookMessage));
}
Also used : Email(org.simplejavamail.api.email.Email) OutlookMessage(org.simplejavamail.outlookmessageparser.model.OutlookMessage) EmailFromOutlookMessage(org.simplejavamail.api.internal.outlooksupport.model.EmailFromOutlookMessage) EmailFromOutlookMessage(org.simplejavamail.api.internal.outlooksupport.model.EmailFromOutlookMessage) OutlookFileAttachment(org.simplejavamail.outlookmessageparser.model.OutlookFileAttachment) MimeMessage(jakarta.mail.internet.MimeMessage) OutlookMessageProxy(org.simplejavamail.internal.outlooksupport.internal.model.OutlookMessageProxy) OutlookAttachment(org.simplejavamail.outlookmessageparser.model.OutlookAttachment) OutlookMsgAttachment(org.simplejavamail.outlookmessageparser.model.OutlookMsgAttachment) Map(java.util.Map) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Aggregations

ByteArrayDataSource (jakarta.mail.util.ByteArrayDataSource)35 Test (org.junit.Test)16 MimeMultipart (jakarta.mail.internet.MimeMultipart)11 DataHandler (jakarta.activation.DataHandler)8 InputStream (java.io.InputStream)7 MimeMessage (jakarta.mail.internet.MimeMessage)6 DataSource (jakarta.activation.DataSource)5 MimeBodyPart (jakarta.mail.internet.MimeBodyPart)5 BufferedInputStream (java.io.BufferedInputStream)5 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)4 Response (jakarta.ws.rs.core.Response)4 Email (org.simplejavamail.api.email.Email)4 MessagingException (jakarta.mail.MessagingException)3 File (java.io.File)3 IOException (java.io.IOException)3 NamedDataSource (org.simplejavamail.internal.util.NamedDataSource)3 BodyPart (jakarta.mail.BodyPart)2 Multipart (jakarta.mail.Multipart)2 InternetAddress (jakarta.mail.internet.InternetAddress)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2