Search in sources :

Example 26 with ByteArrayDataSource

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

the class MimeMessageParserTest method testMoveInvalidEmbeddedResourcesToAttachments_NoHtmlNoInvalid.

@Test
public void testMoveInvalidEmbeddedResourcesToAttachments_NoHtmlNoInvalid() throws IOException {
    ParsedMimeMessageComponents parsedComponents = new ParsedMimeMessageComponents();
    parsedComponents.cidMap.put("moo1", new ByteArrayDataSource("moomoo", "text/plain"));
    parsedComponents.cidMap.put("moo2", new ByteArrayDataSource("moomoo", "text/plain"));
    moveInvalidEmbeddedResourcesToAttachments(parsedComponents);
    assertThat(parsedComponents.cidMap).isEmpty();
    assertThat(parsedComponents.attachmentList).extracting("key").containsOnly("moo1", "moo2");
}
Also used : ParsedMimeMessageComponents(org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.ParsedMimeMessageComponents) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 27 with ByteArrayDataSource

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

the class EmailPopulatingBuilderImpl1Test method testWithEmbeddedImages.

@Test
public void testWithEmbeddedImages() throws IOException {
    List<AttachmentResource> embeddedImages = new ArrayList<>();
    embeddedImages.add(new AttachmentResource("attachment1", getDataSource("blahblah.txt")));
    embeddedImages.add(new AttachmentResource(null, getDataSource("blahblah.txt")));
    embeddedImages.add(new AttachmentResource("attachment1", new ByteArrayDataSource("", "text/text")));
    Email email = builder.withEmbeddedImages(embeddedImages).buildEmail();
    EmailAssert.assertThat(email).hasOnlyEmbeddedImages(embeddedImages);
}
Also used : AttachmentResource(org.simplejavamail.api.email.AttachmentResource) Email(org.simplejavamail.api.email.Email) ArrayList(java.util.ArrayList) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 28 with ByteArrayDataSource

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

the class EmailPopulatingBuilderImpl1Test method testBuilderEmbeddingImages_UnhappyScenario.

@Test
public void testBuilderEmbeddingImages_UnhappyScenario() {
    builder.withEmbeddedImage("a", new ByteArrayDataSource(new byte[3], "")).withEmbeddedImage(null, new DataSourceWithDummyName()).withEmbeddedImage("a", new byte[3], "mimetype");
    try {
        builder.withEmbeddedImage(null, new ByteArrayDataSource(new byte[3], ""));
        failBecauseExceptionWasNotThrown(EmailException.class);
    } catch (EmailException e) {
    // ok
    }
    try {
        // noinspection ConstantConditions
        builder.withEmbeddedImage(null, new byte[3], "mimetype");
        failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
    } catch (IllegalArgumentException e) {
    // ok
    }
}
Also used : ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 29 with ByteArrayDataSource

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

the class EmailPopulatingBuilderUsingDefaultsFromPropertyFileTest method testBuilderSimpleBuildWithStandardEmail_PlusOptionals.

@Test
public void testBuilderSimpleBuildWithStandardEmail_PlusOptionals() throws IOException {
    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==";
    final Email email = EmailBuilder.startingBlank().from("lollypop", "lol.pop@somemail.com").withReplyTo("lollypop-reply", "lol.pop.reply@somemail.com").withBounceTo("lollypop-bounce", "lol.pop.bounce@somemail.com").to("C.Cane", "candycane@candyshop.org").withPlainText("We should meet up!").withHTMLText("<b>We should meet up!</b><img src='cid:thumbsup'>").withSubject("hey").withAttachment("dresscode.txt", namedAttachment).withAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain").withEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png").withDispositionNotificationTo("simple@address.com").withReturnReceiptTo("Complex Email", "simple@address.com").withHeader("dummyHeader", "dummyHeaderValue").withHeader("dummyHeader", "dummyHeaderValueSecond").withHeader("anotherDummyHeader", "anotherDummyHeaderValue").buildEmail();
    assertThat(EmailHelper.createDummyEmailBuilder(true, false, true, false, false, false).buildEmail()).isEqualTo(email);
}
Also used : Email(org.simplejavamail.api.email.Email) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 30 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project tomee by apache.

the class AttachmentTest method testAttachmentViaWsInterface.

// END SNIPPET: setup
/**
 * Create a webservice client using wsdl url
 *
 * @throws Exception
 */
// START SNIPPET: webservice
public void testAttachmentViaWsInterface() throws Exception {
    Service service = Service.create(new URL("http://localhost:" + port + "/webservice-attachments/AttachmentImpl?wsdl"), new QName("http://superbiz.org/wsdl", "AttachmentWsService"));
    assertNotNull(service);
    AttachmentWs ws = service.getPort(AttachmentWs.class);
    // retrieve the SOAPBinding
    SOAPBinding binding = (SOAPBinding) ((BindingProvider) ws).getBinding();
    binding.setMTOMEnabled(true);
    String request = "tsztelak@gmail.com";
    // Byte array
    String response = ws.stringFromBytes(request.getBytes());
    assertEquals(request, response);
    // Data Source
    DataSource source = new ByteArrayDataSource(request.getBytes(), "text/plain; charset=UTF-8");
    // not yet supported !
    // response = ws.stringFromDataSource(source);
    // assertEquals(request, response);
    // Data Handler
    response = ws.stringFromDataHandler(new DataHandler(source));
    assertEquals(request, response);
}
Also used : QName(javax.xml.namespace.QName) Service(jakarta.xml.ws.Service) SOAPBinding(jakarta.xml.ws.soap.SOAPBinding) DataHandler(jakarta.activation.DataHandler) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) URL(java.net.URL) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) DataSource(jakarta.activation.DataSource)

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