Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.mail;

import java.time.Instant;
import java.util.Date;

/**
Expand Down Expand Up @@ -53,6 +54,16 @@ public interface MailMessage {

void setSentDate(Date sentDate) throws MailParseException;

/**
* Set the sent-date of the message.
* @param sentDate the date to set (never {@code null})
* @throws MailParseException in case of errors
* @since 7.1
*/
default void setSentDate(Instant sentDate) throws MailParseException {
setSentDate(Date.from(sentDate));
}

void setSubject(String subject) throws MailParseException;

void setText(String text) throws MailParseException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.mail.javamail;

import java.time.Instant;
import java.util.Date;

import jakarta.mail.MessagingException;
Expand Down Expand Up @@ -163,6 +164,16 @@ public void setSentDate(Date sentDate) throws MailParseException {
}
}

@Override
public void setSentDate(Instant sentDate) throws MailParseException {
try {
this.helper.setSentDate(sentDate);
}
catch (MessagingException ex) {
throw new MailParseException(ex);
}
}

@Override
public void setSubject(String subject) throws MailParseException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.time.Instant;
import java.util.Date;

import jakarta.activation.DataHandler;
Expand Down Expand Up @@ -763,6 +764,17 @@ public void setSentDate(Date sentDate) throws MessagingException {
this.mimeMessage.setSentDate(sentDate);
}

/**
* Set the sent-date of the message.
* @param sentDate the date to set (never {@code null})
* @throws MessagingException in case of errors
* @since 7.1
*/
public void setSentDate(Instant sentDate) throws MessagingException {
Assert.notNull(sentDate, "Sent date must not be null");
this.mimeMessage.setSentDate(Date.from(sentDate));
}

/**
* Set the subject of the message, using the correct encoding.
* @param subject the subject text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.time.Instant;
import java.util.List;
import java.util.Properties;
import java.util.stream.Stream;
Expand Down Expand Up @@ -452,6 +453,28 @@ void connectionWithFailure() {
assertThatExceptionOfType(MessagingException.class).isThrownBy(sender::testConnection);
}

@Test
void javaTimeSentDateSupport() throws Exception {
Instant sentDate = Instant.parse("2026-06-20T10:15:30.00Z");
Date expectedDate = Date.from(sentDate);

// Test MimeMessageHelper
MimeMessage mimeMessage = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setSentDate(sentDate);
assertThat(mimeMessage.getSentDate()).isEqualTo(expectedDate);

// Test MimeMailMessage
MimeMailMessage mimeMailMessage = new MimeMailMessage(helper);
mimeMailMessage.setSentDate(sentDate);
assertThat(mimeMessage.getSentDate()).isEqualTo(expectedDate);

// Test SimpleMailMessage (via MailMessage interface default method)
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSentDate(sentDate);
assertThat(simpleMailMessage.getSentDate()).isEqualTo(expectedDate);
}


private static Stream<String> addresses(Address[] addresses) {
return Arrays.stream(addresses).map(InternetAddress.class::cast).map(InternetAddress::getAddress);
Expand Down