Search in sources :

Example 6 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project resteasy by resteasy.

the class MimeMultipartProviderTest method testXop.

/**
 * @tpTestDetails Custom jaxb object in put request with @XopWithMultipartRelated
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testXop() throws Exception {
    MimeMultipartProviderClient proxy = ProxyBuilder.builder(MimeMultipartProviderClient.class, client.target(generateURL(""))).build();
    MimeMultipartProviderResource.Xop xop = new MimeMultipartProviderResource.Xop(new MimeMultipartProviderCustomer("bill\u00E9"), new MimeMultipartProviderCustomer("monica"), "Hello Xop World!".getBytes(StandardCharsets.UTF_8), new DataHandler(new ByteArrayDataSource("Hello Xop World!".getBytes(StandardCharsets.UTF_8), MediaType.APPLICATION_OCTET_STREAM)));
    proxy.putXop(xop);
}
Also used : MimeMultipartProviderResource(org.jboss.resteasy.test.providers.multipart.resource.MimeMultipartProviderResource) MimeMultipartProviderClient(org.jboss.resteasy.test.providers.multipart.resource.MimeMultipartProviderClient) MimeMultipartProviderCustomer(org.jboss.resteasy.test.providers.multipart.resource.MimeMultipartProviderCustomer) DataHandler(jakarta.activation.DataHandler) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 7 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project triplea by triplea-game.

the class DefaultEmailSender method sendEmail.

@Override
public void sendEmail(final String subject, final String htmlMessage, final Path saveGame, final String saveGameName) throws IOException {
    // this is the last step and we create the email to send
    final Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    if (encrypted) {
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
    }
    props.put("mail.smtp.host", emailServerHost);
    props.put("mail.smtp.port", emailServerPort);
    props.put("mail.smtp.connectiontimeout", TIMEOUT);
    props.put("mail.smtp.timeout", TIMEOUT);
    // todo get the turn and player number from the game data
    try {
        final Session session = Session.getInstance(props, null);
        final MimeMessage mimeMessage = new MimeMessage(session);
        // Build the message fields one by one:
        // priority
        mimeMessage.setHeader("X-Priority", "3 (Normal)");
        // from
        mimeMessage.setFrom(new InternetAddress(username));
        // to address
        for (final String token : Splitter.on(' ').omitEmptyStrings().trimResults().split(toAddress)) {
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(token));
        }
        // subject
        mimeMessage.setSubject(subjectPrefix + " " + subject);
        final MimeBodyPart bodypart = new MimeBodyPart();
        bodypart.setText(htmlMessage, "UTF-8");
        bodypart.setHeader("Content-Type", "text/html");
        if (saveGame != null) {
            final Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(bodypart);
            // add save game
            try (InputStream fin = Files.newInputStream(saveGame)) {
                final DataSource source = new ByteArrayDataSource(fin, "application/triplea");
                final BodyPart messageBodyPart = new MimeBodyPart();
                messageBodyPart.setDataHandler(new DataHandler(source));
                messageBodyPart.setFileName(saveGameName);
                multipart.addBodyPart(messageBodyPart);
            }
            mimeMessage.setContent(multipart);
        }
        // date
        try {
            mimeMessage.setSentDate(Date.from(Instant.now()));
        } catch (final Exception e) {
        // NoOp - the Date field is simply ignored in this case
        }
        try (Transport transport = session.getTransport("smtp")) {
            transport.connect(emailServerHost, emailServerPort, username, password);
            mimeMessage.saveChanges();
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
        }
    } catch (final MessagingException e) {
        throw new IOException(e);
    }
}
Also used : MimeBodyPart(jakarta.mail.internet.MimeBodyPart) BodyPart(jakarta.mail.BodyPart) InternetAddress(jakarta.mail.internet.InternetAddress) Multipart(jakarta.mail.Multipart) MimeMultipart(jakarta.mail.internet.MimeMultipart) MessagingException(jakarta.mail.MessagingException) InputStream(java.io.InputStream) DataHandler(jakarta.activation.DataHandler) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) MessagingException(jakarta.mail.MessagingException) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) DataSource(jakarta.activation.DataSource) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMultipart(jakarta.mail.internet.MimeMultipart) MimeBodyPart(jakarta.mail.internet.MimeBodyPart) Transport(jakarta.mail.Transport) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Session(jakarta.mail.Session)

Example 8 with ByteArrayDataSource

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

the class MimeMessageParserTest method testMoveInvalidEmbeddedResourcesToAttachments_Invalid.

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

Example 9 with ByteArrayDataSource

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

the class EmailPopulatingBuilderImpl1Test method getDataSource.

@SuppressWarnings("SameParameterValue")
private ByteArrayDataSource getDataSource(@Nullable String name) throws IOException {
    ByteArrayDataSource ds = new ByteArrayDataSource("", "text/text");
    ds.setName(name);
    return ds;
}
Also used : ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Example 10 with ByteArrayDataSource

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

the class EmailPopulatingBuilderUsingDefaultsFromPropertyFileTest method testBuilderSimpleBuildWithStandardEmail.

@Test
public void testBuilderSimpleBuildWithStandardEmail() 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==";
    fixLoadedPropertyPath(Property.SMIME_ENCRYPTION_CERTIFICATE);
    final Email email = EmailBuilder.startingBlank().from("lollypop", "lol.pop@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").buildEmail();
    assertThat(EmailHelper.createDummyEmailBuilder(true, true, false, false, false, false).buildEmail()).isEqualTo(email);
}
Also used : Email(org.simplejavamail.api.email.Email) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

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