Search in sources :

Example 11 with MimeMessage

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

Example 12 with MimeMessage

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");
}
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) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 13 with MimeMessage

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);
}
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 14 with 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);
}
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

MimeMessage (jakarta.mail.internet.MimeMessage)14 InternetAddress (jakarta.mail.internet.InternetAddress)10 Test (org.junit.jupiter.api.Test)8 MessagingException (jakarta.mail.MessagingException)5 Address (jakarta.mail.Address)4 Session (jakarta.mail.Session)4 Date (java.util.Date)4 Properties (java.util.Properties)4 MailSendException (org.springframework.mail.MailSendException)4 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)4 NoSuchProviderException (jakarta.mail.NoSuchProviderException)3 Transport (jakarta.mail.Transport)3 ArrayList (java.util.ArrayList)3 GregorianCalendar (java.util.GregorianCalendar)3 MailParseException (org.springframework.mail.MailParseException)3 AuthenticationFailedException (jakarta.mail.AuthenticationFailedException)2 MailAuthenticationException (org.springframework.mail.MailAuthenticationException)2 MailException (org.springframework.mail.MailException)2 MailPreparationException (org.springframework.mail.MailPreparationException)2 FileTypeMap (jakarta.activation.FileTypeMap)1