Search in sources :

Example 1 with Address

use of jakarta.mail.Address 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 Address

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

the class JavaMailSenderImpl method doSend.

/**
 * Actually send the given array of MimeMessages via JavaMail.
 * @param mimeMessages the MimeMessage objects to send
 * @param originalMessages corresponding original message objects
 * that the MimeMessages have been created from (with same array
 * length and indices as the "mimeMessages" array), if any
 * @throws org.springframework.mail.MailAuthenticationException
 * in case of authentication failure
 * @throws org.springframework.mail.MailSendException
 * in case of failure when sending a message
 */
protected void doSend(MimeMessage[] mimeMessages, @Nullable Object[] originalMessages) throws MailException {
    Map<Object, Exception> failedMessages = new LinkedHashMap<>();
    Transport transport = null;
    try {
        for (int i = 0; i < mimeMessages.length; i++) {
            // Check transport connection first...
            if (transport == null || !transport.isConnected()) {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                    // Ignore - we're reconnecting anyway
                    }
                    transport = null;
                }
                try {
                    transport = connectTransport();
                } catch (AuthenticationFailedException ex) {
                    throw new MailAuthenticationException(ex);
                } catch (Exception ex) {
                    // Effectively, all remaining messages failed...
                    for (int j = i; j < mimeMessages.length; j++) {
                        Object original = (originalMessages != null ? originalMessages[j] : mimeMessages[j]);
                        failedMessages.put(original, ex);
                    }
                    throw new MailSendException("Mail server connection failed", ex, failedMessages);
                }
            }
            // Send message via current transport...
            MimeMessage mimeMessage = mimeMessages[i];
            try {
                if (mimeMessage.getSentDate() == null) {
                    mimeMessage.setSentDate(new Date());
                }
                String messageId = mimeMessage.getMessageID();
                mimeMessage.saveChanges();
                if (messageId != null) {
                    // Preserve explicitly specified message id...
                    mimeMessage.setHeader(HEADER_MESSAGE_ID, messageId);
                }
                Address[] addresses = mimeMessage.getAllRecipients();
                transport.sendMessage(mimeMessage, (addresses != null ? addresses : new Address[0]));
            } catch (Exception ex) {
                Object original = (originalMessages != null ? originalMessages[i] : mimeMessage);
                failedMessages.put(original, ex);
            }
        }
    } finally {
        try {
            if (transport != null) {
                transport.close();
            }
        } catch (Exception ex) {
            if (!failedMessages.isEmpty()) {
                throw new MailSendException("Failed to close server connection after message failures", ex, failedMessages);
            } else {
                throw new MailSendException("Failed to close server connection after message sending", ex);
            }
        }
    }
    if (!failedMessages.isEmpty()) {
        throw new MailSendException(failedMessages);
    }
}
Also used : MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MailSendException(org.springframework.mail.MailSendException) Address(jakarta.mail.Address) AuthenticationFailedException(jakarta.mail.AuthenticationFailedException) 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) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) MimeMessage(jakarta.mail.internet.MimeMessage) Transport(jakarta.mail.Transport)

Example 3 with Address

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

Aggregations

Address (jakarta.mail.Address)3 MimeMessage (jakarta.mail.internet.MimeMessage)3 InternetAddress (jakarta.mail.internet.InternetAddress)2 Date (java.util.Date)2 Test (org.junit.jupiter.api.Test)2 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)2 AuthenticationFailedException (jakarta.mail.AuthenticationFailedException)1 MessagingException (jakarta.mail.MessagingException)1 NoSuchProviderException (jakarta.mail.NoSuchProviderException)1 Transport (jakarta.mail.Transport)1 GregorianCalendar (java.util.GregorianCalendar)1 LinkedHashMap (java.util.LinkedHashMap)1 MailAuthenticationException (org.springframework.mail.MailAuthenticationException)1 MailException (org.springframework.mail.MailException)1 MailParseException (org.springframework.mail.MailParseException)1 MailPreparationException (org.springframework.mail.MailPreparationException)1 MailSendException (org.springframework.mail.MailSendException)1