I have a Tomcat application that needs to send confirmation emails etc. I have coded the emailer with Javamail (mail.jar) to send multipart text/html emails. I based the code on the Java EE examples. I'm using the SMTP MTA on the local server.
It works great. In Outlook, I see the HTML version. If I drag it into the Outlook spam folder, I see the text version. So I interpret that as saying it works.
However, if I view the emails in Gmail, I see only the text version. I know the HTML is there (that's where Outlook got it from). But Gmail is not showing it... I have lots of emails from other systems that show as HTML in Gmail.
Can anyone point me to the spec that shows what I am missing? Are there special headers I need to create?
Thanks
Code looks like this:
Message message = new MimeMessage(session);
Multipart multiPart = new MimeMultipart("alternative");
try {
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(text, "utf-8");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html; charset=utf-8");
multiPart.addBodyPart(htmlPart);
multiPart.addBodyPart(textPart);
message.setContent(multiPart);
if(from != null){
message.setFrom(new InternetAddress(from));
}else
message.setFrom();
if(replyto != null)
message.setReplyTo(new InternetAddress[]{new InternetAddress(replyto)});
else
message.setReplyTo(new InternetAddress[]{new InternetAddress(from)});
InternetAddress[] toAddresses = { new InternetAddress(to) };
message.setRecipients(Message.RecipientType.TO, toAddresses);
message.setSubject(subject);
message.setSentDate(new Date());
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
System.out.println("Error: "+e.getMessage());
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("Error: "+e.getMessage());
} finally {
System.out.println("Email sent!");
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…