(no idea if you're still having this problem after all this time, but figured I'd post my solution here in case others find it like I did using Google)
I was having a similar problem, and eventually figured out the problem was that there were multiple versions of javax.mail.* on my classpath. In my particular case, Apache CXF was erroneously including (through Maven) geronimo-javamail_1.4_spec. As soon as I got rid of it, everything worked beautifully :).
prosen2012 wrote:
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));
--------------------------