当我收到JavaMail api发送的电子邮件时,css不起作用,只显示html。
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);
当我收到新邮件并在gmail或outlook中打开时,只会呈现html代码。 css风格不适用......
答案 0 :(得分:3)
通常,电子邮件客户端不允许您使用任何任意CSS。 This article将帮助您了解哪个客户支持哪些内容。
答案 1 :(得分:0)
我认为java邮件中的html是从body
标签开始的,并且无法读取它必须在标签样式中的css样式it-self
试试这个
<html>
<body style=" 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;">
....html stuff
</body>
</html>