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");
}
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);
}
}
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");
}
Aggregations