use of jakarta.mail.internet.MimeMessage in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithParseExceptionOnMimeMessagePreparator.
@Test
public void javaMailSenderWithParseExceptionOnMimeMessagePreparator() {
MockJavaMailSender sender = new MockJavaMailSender();
MimeMessagePreparator preparator = mimeMessage -> mimeMessage.setFrom(new InternetAddress(""));
try {
sender.send(preparator);
} catch (MailParseException ex) {
// expected
boolean condition = ex.getCause() instanceof AddressException;
assertThat(condition).isTrue();
}
}
use of jakarta.mail.internet.MimeMessage in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithSimpleMessage.
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setPort(30);
sender.setUsername("username");
sender.setPassword("password");
SimpleMailMessage simpleMessage = new SimpleMailMessage();
simpleMessage.setFrom("me@mail.org");
simpleMessage.setReplyTo("reply@mail.org");
simpleMessage.setTo("you@mail.org");
simpleMessage.setCc("he@mail.org", "she@mail.org");
simpleMessage.setBcc("us@mail.org", "them@mail.org");
Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
simpleMessage.setSentDate(sentDate);
simpleMessage.setSubject("my subject");
simpleMessage.setText("my text");
sender.send(simpleMessage);
assertThat(sender.transport.getConnectedHost()).isEqualTo("host");
assertThat(sender.transport.getConnectedPort()).isEqualTo(30);
assertThat(sender.transport.getConnectedUsername()).isEqualTo("username");
assertThat(sender.transport.getConnectedPassword()).isEqualTo("password");
assertThat(sender.transport.isCloseCalled()).isTrue();
assertThat(sender.transport.getSentMessages().size()).isEqualTo(1);
MimeMessage sentMessage = sender.transport.getSentMessage(0);
List<Address> froms = Arrays.asList(sentMessage.getFrom());
assertThat(froms.size()).isEqualTo(1);
assertThat(((InternetAddress) froms.get(0)).getAddress()).isEqualTo("me@mail.org");
List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
assertThat(((InternetAddress) replyTos.get(0)).getAddress()).isEqualTo("reply@mail.org");
List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
assertThat(tos.size()).isEqualTo(1);
assertThat(((InternetAddress) tos.get(0)).getAddress()).isEqualTo("you@mail.org");
List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
assertThat(ccs.size()).isEqualTo(2);
assertThat(((InternetAddress) ccs.get(0)).getAddress()).isEqualTo("he@mail.org");
assertThat(((InternetAddress) ccs.get(1)).getAddress()).isEqualTo("she@mail.org");
List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
assertThat(bccs.size()).isEqualTo(2);
assertThat(((InternetAddress) bccs.get(0)).getAddress()).isEqualTo("us@mail.org");
assertThat(((InternetAddress) bccs.get(1)).getAddress()).isEqualTo("them@mail.org");
assertThat(sentMessage.getSentDate().getTime()).isEqualTo(sentDate.getTime());
assertThat(sentMessage.getSubject()).isEqualTo("my subject");
assertThat(sentMessage.getContent()).isEqualTo("my text");
}
use of jakarta.mail.internet.MimeMessage 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.internet.MimeMessage 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