Search in sources :

Example 1 with Session

use of jakarta.mail.Session in project athenz by yahoo.

the class EmailNotificationService method getMimeMessage.

private MimeMessage getMimeMessage(String subject, String body, Collection<String> recipients, String from, byte[] logoImage) throws MessagingException {
    Session session = Session.getDefaultInstance(new Properties());
    // Create a new MimeMessage object.
    MimeMessage message = new MimeMessage(session);
    // Add subject, from and to lines.
    message.setSubject(subject, CHARSET_UTF_8);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(jakarta.mail.Message.RecipientType.BCC, InternetAddress.parse(String.join(",", recipients)));
    // Set the HTML part.
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(body, "text/html; charset=" + CHARSET_UTF_8);
    // Create a multipart/mixed parent container.
    MimeMultipart msgParent = new MimeMultipart("related");
    // Add the body to the message.
    msgParent.addBodyPart(htmlPart);
    // Add the parent container to the message.
    message.setContent(msgParent);
    if (logoImage != null) {
        MimeBodyPart logo = new MimeBodyPart();
        logo.setContent(logoImage, "image/png");
        logo.setContentID(HTML_LOGO_CID_PLACEHOLDER);
        logo.setDisposition(Part.INLINE);
        // Add the attachment to the message.
        msgParent.addBodyPart(logo);
    }
    return message;
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMultipart(jakarta.mail.internet.MimeMultipart) MimeBodyPart(jakarta.mail.internet.MimeBodyPart) Session(jakarta.mail.Session)

Example 2 with Session

use of jakarta.mail.Session in project spring-boot by spring-projects.

the class MailSenderAutoConfigurationTests method jndiSessionAvailableWithResourceRef.

@Test
void jndiSessionAvailableWithResourceRef() {
    Session session = configureJndiSession("java:comp/env/foo");
    testJndiSessionLookup(session, "foo");
}
Also used : Session(jakarta.mail.Session) Test(org.junit.jupiter.api.Test)

Example 3 with Session

use of jakarta.mail.Session in project spring-boot by spring-projects.

the class MailSenderAutoConfigurationTests method jndiSessionAvailable.

@Test
void jndiSessionAvailable() {
    Session session = configureJndiSession("java:comp/env/foo");
    testJndiSessionLookup(session, "java:comp/env/foo");
}
Also used : Session(jakarta.mail.Session) Test(org.junit.jupiter.api.Test)

Example 4 with Session

use of jakarta.mail.Session in project spring-boot by spring-projects.

the class MailSenderAutoConfigurationTests method jndiSessionTakesPrecedenceOverProperties.

@Test
void jndiSessionTakesPrecedenceOverProperties() {
    Session session = configureJndiSession("foo");
    this.contextRunner.withPropertyValues("spring.mail.jndi-name:foo", "spring.mail.host:localhost").run((context) -> {
        assertThat(context).hasSingleBean(Session.class);
        Session sessionBean = context.getBean(Session.class);
        assertThat(sessionBean).isEqualTo(session);
        assertThat(context.getBean(JavaMailSenderImpl.class).getSession()).isEqualTo(sessionBean);
    });
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Session(jakarta.mail.Session) Test(org.junit.jupiter.api.Test)

Example 5 with Session

use of jakarta.mail.Session in project spring-boot by spring-projects.

the class MailSenderAutoConfigurationTests method testJndiSessionLookup.

private void testJndiSessionLookup(Session session, String jndiName) {
    this.contextRunner.withPropertyValues("spring.mail.jndi-name:" + jndiName).run((context) -> {
        assertThat(context).hasSingleBean(Session.class);
        Session sessionBean = context.getBean(Session.class);
        assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
        assertThat(sessionBean).isEqualTo(session);
        assertThat(context.getBean(JavaMailSenderImpl.class).getSession()).isEqualTo(sessionBean);
    });
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Session(jakarta.mail.Session)

Aggregations

Session (jakarta.mail.Session)9 Test (org.junit.jupiter.api.Test)5 Properties (java.util.Properties)4 InternetAddress (jakarta.mail.internet.InternetAddress)3 MimeMessage (jakarta.mail.internet.MimeMessage)3 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)3 Provider (jakarta.mail.Provider)1 MimeBodyPart (jakarta.mail.internet.MimeBodyPart)1 MimeMultipart (jakarta.mail.internet.MimeMultipart)1 GregorianCalendar (java.util.GregorianCalendar)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1