Search in sources :

Example 1 with InternetAddress

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");
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) InternetAddress(jakarta.mail.internet.InternetAddress) Address(jakarta.mail.Address) MimeMessage(jakarta.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test)

Example 2 with InternetAddress

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);
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) MimeMessage(jakarta.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test)

Example 3 with InternetAddress

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);
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) MimeMessage(jakarta.mail.internet.MimeMessage) Test(org.junit.jupiter.api.Test)

Example 4 with InternetAddress

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");
    }
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) MailSendException(org.springframework.mail.MailSendException) MimeMessage(jakarta.mail.internet.MimeMessage) MessagingException(jakarta.mail.MessagingException) Test(org.junit.jupiter.api.Test)

Example 5 with InternetAddress

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();
    }
}
Also used : Arrays(java.util.Arrays) MailParseException(org.springframework.mail.MailParseException) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) FileTypeMap(jakarta.activation.FileTypeMap) AddressException(jakarta.mail.internet.AddressException) InternetAddress(jakarta.mail.internet.InternetAddress) MessagingException(jakarta.mail.MessagingException) MimeMessage(jakarta.mail.internet.MimeMessage) ArrayList(java.util.ArrayList) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Session(jakarta.mail.Session) Properties(java.util.Properties) GregorianCalendar(java.util.GregorianCalendar) NoSuchProviderException(jakarta.mail.NoSuchProviderException) ObjectUtils(org.springframework.util.ObjectUtils) IOException(java.io.IOException) Message(jakarta.mail.Message) Assertions.entry(org.assertj.core.api.Assertions.entry) URLName(jakarta.mail.URLName) Test(org.junit.jupiter.api.Test) List(java.util.List) MailSendException(org.springframework.mail.MailSendException) Transport(jakarta.mail.Transport) Address(jakarta.mail.Address) InternetAddress(jakarta.mail.internet.InternetAddress) AddressException(jakarta.mail.internet.AddressException) MailParseException(org.springframework.mail.MailParseException) Test(org.junit.jupiter.api.Test)

Aggregations

InternetAddress (jakarta.mail.internet.InternetAddress)9 Test (org.junit.jupiter.api.Test)9 MimeMessage (jakarta.mail.internet.MimeMessage)8 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)4 Address (jakarta.mail.Address)3 MessagingException (jakarta.mail.MessagingException)3 Session (jakarta.mail.Session)3 GregorianCalendar (java.util.GregorianCalendar)3 Properties (java.util.Properties)3 MailSendException (org.springframework.mail.MailSendException)3 Date (java.util.Date)2 FileTypeMap (jakarta.activation.FileTypeMap)1 Message (jakarta.mail.Message)1 NoSuchProviderException (jakarta.mail.NoSuchProviderException)1 Transport (jakarta.mail.Transport)1 URLName (jakarta.mail.URLName)1 AddressException (jakarta.mail.internet.AddressException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1