java - 在 java 中包含 css(用于 href 标签)

标签 java css tags href

我必须将样式应用到我的邮件内容(用于 href 标签)。我的代码如下。

public void sendMailWithHsHeader(SentEmailDTO sendEmailDto)throws Exception {
        String css="";
        String mailHeader="";
        String mailfooter="";
        String mailContent=sendEmailDto.getContent();

        css="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
        css+="<html xmlns='http://www.w3.org/1999/xhtml'>";
        css+="<head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>";
        css+="<style type='text/css'>";
        css+="a {color:#CCC; text-decoration:none; padding:0px 5px; font-size:.9em;}";
        css+="a:hover {color:#9F0;}";
        css+="</style></head><body>";

        mailHeader="<table cellspacing='0' cellpadding='0' border='0' align='center' width='625'>"; 
        mailHeader+="<tr><td align='left' valign='top' style='border-top:1px solid #CCC;'><img src='http://hiringsteps.com/email/images/border-ul.jpg'/></td>"; 
        mailHeader+="<td style='border-top:1px solid #CCC;'><table cellspacing='0' cellpadding='0' border='0' width='100%'>";
        mailHeader+="<tr><td height='40' align='left'><a href='http://hiringsteps.com'><img border='0'"; 
        mailHeader+="src='http://hiringsteps.com/email/images/hiring-steps.jpg'/></a></td></tr><tr><td align='center' style='background-color:#1E314E;'><img src='http://hiringsteps.com/email/images/line.jpg'/></td>";        
        mailHeader+="</tr></table></td><td align='right' valign='top' style='border-top:1px solid #CCC;'><img src='http://hiringsteps.com/email/images/border-ur.jpg'/></td>";
        mailHeader+="</tr><tr><td width='20'></td><td height='300' align='left' valign='top' style='background-color:#F0F0F0; font:14px normal Arial, Helvetica, sans-serif; padding:10px 20px; line-height:1.3em;'>";        

        mailfooter+="</td><td width='20'></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td align='left' valign='bottom' style='border-bottom:1px solid #CCC;'><img src='http://hiringsteps.com/email/images/border-ll.jpg'></td>";
        mailfooter+="<td valign='bottom' style='border-bottom:1px solid #CCC;'><table cellspacing='0' cellpadding='0' border='0' width='100%'>";
        mailfooter+="<tr><td align='center' style='background-color:#1E314E; font:14px normal Arial, Helvetica, sans-serif; padding:10px 20px; line-height:1.3em;'>";        
        mailfooter+="<p><a href='http://hiringsteps.com'>HOME</a><a href='http://hiringsteps.com/Features.html'>FEATURES</a><a href='http://hiringsteps.com/Pricing.html'>PRICING</a><a href='http://hiringsteps.com/About-Us.html'>ABOUT US</a><a href='http://hiringsteps.com/Contact-Us.html'>CONTACT US</a></p>";
        mailfooter+="<p><span style='font-size:.9em; color:#CCC;'>follow us</span> <a href='https://www.facebook.com/pages/Hiring-Steps/155650987881886'><img border='0' align='absmiddle' src='http://hiringsteps.com/email/images/fb.jpg'/></a> <a href='https://twitter.com/#!/Hiringsteps'><img border='0' align='absmiddle' src='http://hiringsteps.com/email/images/tw.jpg'/></a> <a href='http://www.linkedin.com/company/2630770'><img border='0' align='absmiddle' src='http://hiringsteps.com/email/images/in.jpg'/></a> <a href='http://hiringsteps.com/Signin.html'>";
        mailfooter+="<img border='0' align='absmiddle' src='http://hiringsteps.com/email/images/login.jpg'/></a></p></td></tr><tr>";               
        mailfooter+="<td>&nbsp;</td></tr></table></td><td align='right' valign='bottom' style='border-bottom:1px solid #CCC;'><img src='http://hiringsteps.com/email/images/border-lr.jpg'/></td>";       
        mailfooter+="</tr><tr><td>&nbsp;</td><td align='center'><p style='color:#777; font:11px normal Arial, Helvetica, sans-serif;'>&copy; Copyright 2012 HiringSteps. All rights reserved.<br>2321 Rosecrans Avenue, Suite 4270, El Segundo, CA 90245</p></td>";
        mailfooter+="<td>&nbsp;</td></tr></table></body></html>"; 

        sendEmailDto.setContent(css+mailHeader+mailContent+mailfooter);

        Properties props = new Properties();
        setProperties(props);
        Session session = Session.getInstance(props,null);

        session.setDebug(true);
        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(sendEmailDto.getFromAddress());
        msg.setFrom(addressFrom);

        String to = sendEmailDto.getToAddress();
        String[] toAddress = to.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

        InternetAddress[] addressTo = new InternetAddress[toAddress.length];
        for (int i = 0; i < toAddress.length; i++) {
            addressTo[i] = new InternetAddress(toAddress[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        if(sendEmailDto.getCcAddress()!= null) {
            if(!sendEmailDto.getCcAddress().equals("")) {
                String cc = sendEmailDto.getCcAddress(); 
                String[] ccAddress = cc.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

                InternetAddress[] addressCC = new InternetAddress[ccAddress.length];

                for (int i = 0; i < ccAddress.length; i++)
                {
                    addressCC[i] = new InternetAddress(ccAddress[i]);
                }           
                msg.setRecipients(Message.RecipientType.CC, addressCC);
            }
        }           

        InternetAddress[] replyTo = new InternetAddress[1];
        replyTo[0]=new InternetAddress("noreply@hiringsteps.com");      
        msg.setReplyTo(replyTo);
        msg.setSubject(sendEmailDto.getSubject());
        // Now the message body.
        Multipart mp = new MimeMultipart();

        BodyPart pixPart = new MimeBodyPart();
        setBodyParts(sendEmailDto, msg, mp, pixPart);

        Transport.send(msg);
}



public void setBodyParts(SentEmailDTO sendEmailDto, Message msg,Multipart mp, BodyPart pixPart) throws MessagingException {     
        String content=sendEmailDto.getContent();       
        content=content.replace("\n", "<br />\n");
        pixPart.setContent(content, "text/html");
        pixPart.addHeader("Content-type", "text/html; charset=UTF-8");
        mp.addBodyPart(pixPart);
        msg.setContent(mp);     
}

在从 mailFooter 开始的第 4 行中,我必须将样式应用于 css 字符串中的 hreg 标记。但是样式没有得到应用。请帮帮我..

最佳答案

你希望这些坏男孩出现在你的标题中,否则它不知道它会是 HTML 格式的。

内容类型:文本/html; charset=iso-8859-1

关于java - 在 java 中包含 css(用于 href 标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13174955/

相关文章:

java - Android while循环导致应用程序崩溃

JavaX 命名.NameNotFoundException 与 Jboss/Wildfly,这个名字是什么意思?

类似于定格动画的 CSS 关键帧动画

html - 减少文本字段宽度并在文本下方显示评论框

java - 多个线程可以等待同一个对象吗?

java - 使用 JDBC 执行多个 SQL 语句 (CRUD)

javascript - 使用 bootstrap 创建模态表单 (HTML)

python - nltk pos 标签咒语

python - 如何合并两个漂亮的汤标签?

php - 如何修复php中以标签开头的变量