Search in sources :

Example 11 with ByteArrayDataSource

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);
}
Also used : NamedDataSource(org.simplejavamail.internal.util.NamedDataSource) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 12 with ByteArrayDataSource

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);
}
Also used : NamedDataSource(org.simplejavamail.internal.util.NamedDataSource) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 13 with ByteArrayDataSource

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;
}
Also used : AttachmentResource(org.simplejavamail.api.email.AttachmentResource) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMultipart(jakarta.mail.internet.MimeMultipart) Properties(java.util.Properties) MimeBodyPart(jakarta.mail.internet.MimeBodyPart) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Example 14 with ByteArrayDataSource

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Integer.toHexString(java.lang.Integer.toHexString) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with ByteArrayDataSource

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);
        }
    }
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) Multipart(jakarta.mail.Multipart) MimeMultipart(jakarta.mail.internet.MimeMultipart) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMultipart(jakarta.mail.internet.MimeMultipart) DataHandler(jakarta.activation.DataHandler) MimeBodyPart(jakarta.mail.internet.MimeBodyPart) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) BusException(org.bihealth.mi.easybus.BusException) Date(java.util.Date) MessagingException(jakarta.mail.MessagingException) IOException(java.io.IOException) BusException(org.bihealth.mi.easybus.BusException)

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