After receiving an email sent using the JavaMail API, I noticed that the CSS styles are not being applied and only the HTML content is rendered.
HTML CODE
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Title</title>
<style>
.gradiente{
background: -webkit-linear-gradient(#FFFCE8, #FFF8AF); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(#FFFCE8, #FFF8AF); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(#FFFCE8, #FFF8AF); /* For Firefox 3.6 to 15 */
background: linear-gradient(#FFFCE8, #FFF8AF); /* Standard syntax */
background-repeat: repeat;
}
a lot of styles below....... I did not paste all of it
</style>
</head>
<body class='gradiente'>
....HTML stuff
</body>
</html>
JAVA API
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", porta);
MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/css;; x-java-content-handler=com.sun.mail.handlers.text_css");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, passWord);
}
});
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
if(cc != null){
Iterator<String> ccIt = cc.iterator();
while (ccIt.hasNext()) {
String itemcc = ccIt.next();
message.addRecipient(Message.RecipientType.CC,
new InternetAddress(itemcc));
}
}
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Set Subject: header field
message.setSubject(subject);
// Send the actual HTML message, as big as you like
Iterator<String> it = content.iterator();
String textoConteudo="";
while (it.hasNext()) {
String item = it.next();
textoConteudo = textoConteudo + item;
}
messageBodyPart.setContent(textoConteudo,
"text/html; charset=iso-8859-1" );
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
if(fileName !=null){
Iterator<String> itFN = fileName.iterator();
while (itFN.hasNext()) {
String arq = itFN.next();
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(arq);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(arq);
multipart.addBodyPart(messageBodyPart);
}
}
// Send the complete message parts
message.setContent(multipart );
// Send message
Transport.send(message);
Upon opening the email in Gmail or Outlook, I observed that only the HTML code is displayed without applying the CSS styles.