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

时间:2012-11-01 10:17:26

标签: 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);     
}

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

1 个答案:

答案 0 :(得分:2)

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

Content-type: text/html; charset=iso-8859-1