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;
}
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);
}
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);
}
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);
}
Aggregations