use of jakarta.mail.util.ByteArrayDataSource in project simple-java-mail by bbottema.
the class EmailTest method testEqualsEmail_EqualityEmbeddedImages.
@Test
public void testEqualsEmail_EqualityEmbeddedImages() throws IOException {
assertEmailEqual(b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").buildEmail(), b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").buildEmail(), true);
assertEmailEqual(b().withEmbeddedImage(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/png"))).buildEmail(), b().withEmbeddedImage(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/png"))).buildEmail(), true);
assertEmailEqual(b().withEmbeddedImage("name", new ByteArrayDataSource("data", "image/png")).buildEmail(), b().withEmbeddedImage("name", new ByteArrayDataSource("data", "image/png")).buildEmail(), true);
assertEmailEqual(b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").withEmbeddedImage("name2", new byte[] { 'b' }, "image/bmp").buildEmail(), b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").withEmbeddedImage("name2", new byte[] { 'b' }, "image/bmp").buildEmail(), true);
assertEmailEqual(b().withEmbeddedImage(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/png"))).buildEmail(), b().withEmbeddedImage(null, new NamedDataSource("n2", new ByteArrayDataSource("data", "image/png"))).buildEmail(), false);
assertEmailEqual(b().withEmbeddedImage(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/png"))).buildEmail(), b().withEmbeddedImage(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/jpg"))).buildEmail(), false);
assertEmailEqual(b().withEmbeddedImage("name", new ByteArrayDataSource("data", "image/png")).buildEmail(), b().withEmbeddedImage("nameOther", new ByteArrayDataSource("data", "image/png")).buildEmail(), false);
assertEmailEqual(b().withEmbeddedImage("name", new ByteArrayDataSource("data", "image/png")).buildEmail(), b().withEmbeddedImage("name", new ByteArrayDataSource("data", "image/jpg")).buildEmail(), false);
assertEmailEqual(b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").withEmbeddedImage("name2", new byte[] { 'b' }, "image/bmp").buildEmail(), b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").buildEmail(), false);
assertEmailEqual(b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").withEmbeddedImage("name2", new byte[] { 'b' }, "image/bmp").buildEmail(), b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").withEmbeddedImage("name3", new byte[] { 'b' }, "image/bmp").buildEmail(), false);
assertEmailEqual(b().withEmbeddedImage("name", new byte[] { 'a' }, "image/png").buildEmail(), b().buildEmail(), false);
}
use of jakarta.mail.util.ByteArrayDataSource in project simple-java-mail by bbottema.
the class EmailTest method testEqualsEmail_EqualityAttachments.
@Test
public void testEqualsEmail_EqualityAttachments() throws IOException {
assertEmailEqual(b().withAttachment("name", new byte[] { 'a' }, "image/png").buildEmail(), b().withAttachment("name", new byte[] { 'a' }, "image/png").buildEmail(), true);
assertEmailEqual(b().withAttachment(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/png"))).buildEmail(), b().withAttachment(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/png"))).buildEmail(), true);
assertEmailEqual(b().withAttachment("name", new ByteArrayDataSource("data", "image/png")).buildEmail(), b().withAttachment("name", new ByteArrayDataSource("data", "image/png")).buildEmail(), true);
assertEmailEqual(b().withAttachment("name", new byte[] { 'a' }, "image/png").withAttachment("name2", new byte[] { 'b' }, "image/bmp").buildEmail(), b().withAttachment("name", new byte[] { 'a' }, "image/png").withAttachment("name2", new byte[] { 'b' }, "image/bmp").buildEmail(), true);
assertEmailEqual(b().withAttachment(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/png"))).buildEmail(), b().withAttachment(null, new NamedDataSource("n2", new ByteArrayDataSource("data", "image/png"))).buildEmail(), false);
assertEmailEqual(b().withAttachment(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/png"))).buildEmail(), b().withAttachment(null, new NamedDataSource("n1", new ByteArrayDataSource("data", "image/jpg"))).buildEmail(), false);
assertEmailEqual(b().withAttachment("name", new ByteArrayDataSource("data", "image/png")).buildEmail(), b().withAttachment("nameOther", new ByteArrayDataSource("data", "image/png")).buildEmail(), false);
assertEmailEqual(b().withAttachment("name", new ByteArrayDataSource("data", "image/png")).buildEmail(), b().withAttachment("name", new ByteArrayDataSource("data", "image/jpg")).buildEmail(), false);
assertEmailEqual(b().withAttachment("name", new byte[] { 'a' }, "image/png").withAttachment("name2", new byte[] { 'b' }, "image/bmp").buildEmail(), b().withAttachment("name", new byte[] { 'a' }, "image/png").buildEmail(), false);
assertEmailEqual(b().withAttachment("name", new byte[] { 'a' }, "image/png").withAttachment("name2", new byte[] { 'b' }, "image/bmp").buildEmail(), b().withAttachment("name", new byte[] { 'a' }, "image/png").withAttachment("name3", new byte[] { 'b' }, "image/bmp").buildEmail(), false);
assertEmailEqual(b().withAttachment("name", new byte[] { 'a' }, "image/png").buildEmail(), b().buildEmail(), false);
}
use of jakarta.mail.util.ByteArrayDataSource in project simple-java-mail by bbottema.
the class MimeMessageParserTest method produceMimeMessageWithNamingIssue.
private MimeMessage produceMimeMessageWithNamingIssue() throws MessagingException, IOException {
MimeMessage m = new MimeMessage(Session.getDefaultInstance(new Properties()));
MimeMultipart multipartRootMixed = new MimeMultipart("mixed");
// content
final MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText("body text", "UTF-8");
multipartRootMixed.addBodyPart(messagePart);
// attachments
final AttachmentResource r = new AttachmentResource("wrong-name.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"));
multipartRootMixed.addBodyPart(getBodyPartFromDatasource(r, Part.ATTACHMENT));
m.setContent(multipartRootMixed);
return m;
}
use of jakarta.mail.util.ByteArrayDataSource in project simple-java-mail by bbottema.
the class MiscUtil method tryLoadingFromClassPath.
@Nullable
private static DataSource tryLoadingFromClassPath(@NotNull final String resourceName) throws IOException {
final String cleanResourceName = resourceName.replaceAll("//", "/");
final InputStream is = MiscUtil.class.getResourceAsStream(cleanResourceName);
if (is != null) {
try {
final String mimeType = ImageMimeType.getContentType(resourceName);
final ByteArrayDataSource ds = new ByteArrayDataSource(is, mimeType);
// EMAIL-125: set the name of the DataSource to the normalized resource URL similar to other DataSource implementations, e.g. FileDataSource, URLDataSource
ds.setName(MiscUtil.class.getResource(cleanResourceName).toString());
return ds;
} finally {
is.close();
}
}
return null;
}
use of jakarta.mail.util.ByteArrayDataSource in project easy-smpc by prasser.
the class ConnectionIMAP method send.
@Override
protected void send(String recipient, String subject, String body, Object attachment) throws BusException {
synchronized (propertiesSending) {
// Make sure we are ready to go
if (sessionSending == null) {
sessionSending = Session.getInstance(propertiesSending, null);
}
try {
// Create message
MimeMessage email = new MimeMessage(sessionSending);
// Add sender and recipient
email.setRecipient(RecipientType.TO, new InternetAddress(recipient));
email.setSender(new InternetAddress(getSendingEmailAddress()));
email.setFrom(new InternetAddress(getSendingEmailAddress()));
email.setSubject(subject);
// Add body
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDisposition(MimeBodyPart.INLINE);
mimeBodyPart.setContent(body, "text/plain");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
// Add attachment
long attachmentSize = 0;
if (attachment != null) {
mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
byte[] attachmentBytes = getByteArrayOutputStream(attachment);
mimeBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(attachmentBytes, "application/octet-stream")));
mimeBodyPart.setFileName(FILENAME_MESSAGE);
multipart.addBodyPart(mimeBodyPart);
attachmentSize = attachmentBytes.length;
}
// Compose message
email.setContent(multipart);
// Send
Transport.send(email, getSendingUserName(), sendingPassword);
if (listener != null) {
listener.messageSent(attachmentSize);
}
LOGGER.debug("Message sent logged", new Date(), "Message sent", subject);
} catch (Exception e) {
throw new BusException("Unable to send message", e);
}
}
}
Aggregations