use of jakarta.mail.internet.InternetAddress in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithSimpleMessages.
@Test
public void javaMailSenderWithSimpleMessages() throws MessagingException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setUsername("username");
sender.setPassword("password");
SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
simpleMessage1.setTo("he@mail.org");
SimpleMailMessage simpleMessage2 = new SimpleMailMessage();
simpleMessage2.setTo("she@mail.org");
sender.send(simpleMessage1, simpleMessage2);
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(2);
MimeMessage sentMessage1 = sender.transport.getSentMessage(0);
List<Address> tos1 = Arrays.asList(sentMessage1.getRecipients(Message.RecipientType.TO));
assertThat(tos1.size()).isEqualTo(1);
assertThat(((InternetAddress) tos1.get(0)).getAddress()).isEqualTo("he@mail.org");
MimeMessage sentMessage2 = sender.transport.getSentMessage(1);
List<Address> tos2 = Arrays.asList(sentMessage2.getRecipients(Message.RecipientType.TO));
assertThat(tos2.size()).isEqualTo(1);
assertThat(((InternetAddress) tos2.get(0)).getAddress()).isEqualTo("she@mail.org");
}
use of jakarta.mail.internet.InternetAddress in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithMimeMessages.
@Test
public void javaMailSenderWithMimeMessages() throws MessagingException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setUsername("username");
sender.setPassword("password");
MimeMessage mimeMessage1 = sender.createMimeMessage();
mimeMessage1.setRecipient(Message.RecipientType.TO, new InternetAddress("he@mail.org"));
MimeMessage mimeMessage2 = sender.createMimeMessage();
mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress("she@mail.org"));
sender.send(mimeMessage1, mimeMessage2);
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(2);
assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage1);
assertThat(sender.transport.getSentMessage(1)).isEqualTo(mimeMessage2);
}
use of jakarta.mail.internet.InternetAddress in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithMimeMessage.
@Test
public void javaMailSenderWithMimeMessage() throws MessagingException {
MockJavaMailSender sender = new MockJavaMailSender();
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.InternetAddress in project spring-framework by spring-projects.
the class JavaMailSenderTests method failedMimeMessage.
@Test
public void failedMimeMessage() throws MessagingException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setUsername("username");
sender.setPassword("password");
MimeMessage mimeMessage1 = sender.createMimeMessage();
mimeMessage1.setRecipient(Message.RecipientType.TO, new InternetAddress("he@mail.org"));
mimeMessage1.setSubject("fail");
MimeMessage mimeMessage2 = sender.createMimeMessage();
mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress("she@mail.org"));
try {
sender.send(mimeMessage1, mimeMessage2);
} catch (MailSendException ex) {
ex.printStackTrace();
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(mimeMessage2);
assertThat(ex.getFailedMessages().size()).isEqualTo(1);
assertThat(ex.getFailedMessages().keySet().iterator().next()).isEqualTo(mimeMessage1);
Object subEx = ex.getFailedMessages().values().iterator().next();
boolean condition = subEx instanceof MessagingException;
assertThat(condition).isTrue();
assertThat(((MessagingException) subEx).getMessage()).isEqualTo("failed");
}
}
use of jakarta.mail.internet.InternetAddress 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();
}
}
Aggregations