Question

Spring's org.springframework.mail.javamail.MimeMessageHelper.setSentDate(Date date) takes a date that will be set in the header of the email, but not a format. I need to specify a particular format.

I checked the source and MimeMessage uses javax.mail.internet.MailDateFormat to format it, but it doesn't have a way to set the format from the application.

Was it helpful?

Solution

You can alternately set a custom header using the MimeMessage.addHeader(String, String) method, and insert the formatted date using SimpleDateFormat.format(Date):

String dateFormat = "yyyy-MM-dd HH:mm:ss"; // you specify the format for your date
String formattedDate = new SimpleDateFormat(dateFormat).format(new Date());
MimeMessage mimeMessage = //get message from wherever
mimeMessage.addHeader("Date", formattedDate);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top