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);
}
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;
}
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;
}
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);
}
}
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));
}
Aggregations