Search in sources :

Example 6 with Session

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

the class MailSenderAutoConfigurationTests method configureJndiSession.

private Session configureJndiSession(String name) {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    TestableInitialContextFactory.bind(name, session);
    return session;
}
Also used : Properties(java.util.Properties) Session(jakarta.mail.Session)

Example 7 with Session

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

the class MailHealthIndicatorTests method setup.

@BeforeEach
void setup() {
    Session session = Session.getDefaultInstance(new Properties());
    session.addProvider(new Provider(Type.TRANSPORT, "success", SuccessTransport.class.getName(), "Test", "1.0.0"));
    this.mailSender = mock(JavaMailSenderImpl.class);
    given(this.mailSender.getHost()).willReturn("smtp.acme.org");
    given(this.mailSender.getPort()).willReturn(25);
    given(this.mailSender.getSession()).willReturn(session);
    this.indicator = new MailHealthIndicator(this.mailSender);
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties) Session(jakarta.mail.Session) Provider(jakarta.mail.Provider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with Session

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

the class JavaMailSenderTests method javaMailProperties.

@Test
public void javaMailProperties() throws MessagingException {
    Properties props = new Properties();
    props.setProperty("bogusKey", "bogusValue");
    MockJavaMailSender sender = new MockJavaMailSender() {

        @Override
        protected Transport getTransport(Session sess) throws NoSuchProviderException {
            assertThat(sess.getProperty("bogusKey")).isEqualTo("bogusValue");
            return super.getTransport(sess);
        }
    };
    sender.setJavaMailProperties(props);
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    MimeMessage mimeMessage = sender.createMimeMessage();
    mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org"));
    sender.send(mimeMessage);
    assertThat(sender.transport.getConnectedHost()).isEqualTo("host");
    assertThat(sender.transport.getConnectedUsername()).isEqualTo("username");
    assertThat(sender.transport.getConnectedPassword()).isEqualTo("password");
    assertThat(sender.transport.isCloseCalled()).isTrue();
    assertThat(sender.transport.getSentMessages().size()).isEqualTo(1);
    assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage);
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) MimeMessage(jakarta.mail.internet.MimeMessage) Properties(java.util.Properties) Session(jakarta.mail.Session) Test(org.junit.jupiter.api.Test)

Example 9 with Session

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

the class JavaMailSenderTests method javaMailSenderWithCustomSession.

@Test
public void javaMailSenderWithCustomSession() throws MessagingException {
    final Session session = Session.getInstance(new Properties());
    MockJavaMailSender sender = new MockJavaMailSender() {

        @Override
        protected Transport getTransport(Session sess) throws NoSuchProviderException {
            assertThat(sess).isEqualTo(session);
            return super.getTransport(sess);
        }
    };
    sender.setSession(session);
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    MimeMessage mimeMessage = sender.createMimeMessage();
    mimeMessage.setSubject("custom");
    mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org"));
    mimeMessage.setSentDate(new GregorianCalendar(2005, 3, 1).getTime());
    sender.send(mimeMessage);
    assertThat(sender.transport.getConnectedHost()).isEqualTo("host");
    assertThat(sender.transport.getConnectedUsername()).isEqualTo("username");
    assertThat(sender.transport.getConnectedPassword()).isEqualTo("password");
    assertThat(sender.transport.isCloseCalled()).isTrue();
    assertThat(sender.transport.getSentMessages().size()).isEqualTo(1);
    assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage);
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) MimeMessage(jakarta.mail.internet.MimeMessage) GregorianCalendar(java.util.GregorianCalendar) Properties(java.util.Properties) Session(jakarta.mail.Session) Test(org.junit.jupiter.api.Test)

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