All headers missing
I am using the code below to send emails and am encountering a strange problem. Within all environments (maven and tomcat) except when run as junit test in eclipse, all of the headers of my mimemessages are committed during send.
Prior to calling transport.sendMessage() I can enumerate through message.getAllHeaders() and see that all of the headers are present. However when I log what is actually sent, the headers are missing. When I watch what is sent, all of the headers are skipped and only the mime parts are present.
Here is the code:
Session mailSession = Session.getInstance(this.mailProps, null);
Transport transport = mailSession.getTransport();
transport.connect(this.host, this.user, this.password);
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
if (attachmentFilename != null) {
//Create multipart message with file attachment
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(contents);
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentFilename);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(attachmentFilename.substring(attachmentFilename.indexOf("\\") + 1, attachmentFilename.length()));
multipart.addBodyPart(attachmentBodyPart);
message.setContent(multipart);
} else {
message.setContent(contents, "text/plain");
}
for (int i = 0; i < recipients.length; i++) {
String recipient = recipients[i];
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(recipient));
}
message.setFrom(new InternetAddress(sender));
message.setSender(new InternetAddress(sender));
message.setSentDate(new Date(System.currentTimeMillis()));
message.setReplyTo(new Address[]{new InternetAddress(sender)});
Enumeration e=message.getAllHeaderLines();
int i=0;
while(e.hasMoreElements()&&i<50){
log.debug(e.nextElement());
i++;
}
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
--------------------------