Search in sources :

Example 1 with MimeMessage

use of jakarta.mail.internet.MimeMessage in project gocd by gocd.

the class GoSmtpMailSender method send.

@Override
public ValidationBean send(String subject, String body, String to) {
    Transport transport = null;
    try {
        LOGGER.debug("Sending email [{}] to [{}]", subject, to);
        Properties props = mailProperties();
        MailSession session = MailSession.getInstance().createWith(props, mailHost.getUsername(), mailHost.getPassword());
        transport = session.getTransport();
        transport.connect(mailHost.getHostName(), mailHost.getPort(), StringUtils.trimToNull(mailHost.getUsername()), StringUtils.trimToNull(mailHost.getPassword()));
        MimeMessage msg = session.createMessage(mailHost.getFrom(), to, subject, body);
        transport.sendMessage(msg, msg.getRecipients(TO));
        return ValidationBean.valid();
    } catch (Exception e) {
        LOGGER.error("Sending failed for email [{}] to [{}]", subject, to, e);
        return ValidationBean.notValid(ERROR_MESSAGE);
    } finally {
        if (transport != null) {
            try {
                transport.close();
            } catch (MessagingException e) {
                LOGGER.error("Failed to close transport", e);
            }
        }
    }
}
Also used : MimeMessage(jakarta.mail.internet.MimeMessage) MessagingException(jakarta.mail.MessagingException) Transport(jakarta.mail.Transport) Properties(java.util.Properties) MessagingException(jakarta.mail.MessagingException)

Example 2 with MimeMessage

use of jakarta.mail.internet.MimeMessage in project spring-framework by spring-projects.

the class JavaMailSenderImpl method send.

@Override
public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
    try {
        List<MimeMessage> mimeMessages = new ArrayList<>(mimeMessagePreparators.length);
        for (MimeMessagePreparator preparator : mimeMessagePreparators) {
            MimeMessage mimeMessage = createMimeMessage();
            preparator.prepare(mimeMessage);
            mimeMessages.add(mimeMessage);
        }
        send(mimeMessages.toArray(new MimeMessage[0]));
    } catch (MailException ex) {
        throw ex;
    } catch (MessagingException ex) {
        throw new MailParseException(ex);
    } catch (Exception ex) {
        throw new MailPreparationException(ex);
    }
}
Also used : MailPreparationException(org.springframework.mail.MailPreparationException) MimeMessage(jakarta.mail.internet.MimeMessage) MessagingException(jakarta.mail.MessagingException) ArrayList(java.util.ArrayList) MailParseException(org.springframework.mail.MailParseException) MailException(org.springframework.mail.MailException) MailParseException(org.springframework.mail.MailParseException) NoSuchProviderException(jakarta.mail.NoSuchProviderException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MessagingException(jakarta.mail.MessagingException) MailSendException(org.springframework.mail.MailSendException) MailPreparationException(org.springframework.mail.MailPreparationException) MailException(org.springframework.mail.MailException) AuthenticationFailedException(jakarta.mail.AuthenticationFailedException)

Example 3 with MimeMessage

use of jakarta.mail.internet.MimeMessage 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 4 with MimeMessage

use of jakarta.mail.internet.MimeMessage 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 5 with MimeMessage

use of jakarta.mail.internet.MimeMessage 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)

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