2012-11-01 3 views
0

내 메일 내용 (href 태그 용)에 스타일을 적용해야합니다. 내 코드는 아래와 같습니다.자바에서 CSS 포함 (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("[email protected]");  
     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로 시작하여 CSS 문자열에 hreg 태그에 스타일을 적용해야합니다. 그러나 스타일은 적용되지 않습니다. 제발 도와주세요 ..

+1

빈 mailHeader를 보내고 있다는 것을 알고 있습니까? –

+0

예 .. 지금 수정했습니다. 여기서는 가독성을 위해 그냥 건너 뛰었습니다. – 1355

+1

첫 단계 - Java 비트를 잊어 버리십시오. 'css + mailContent + mailFooter'의 값을 캡처하고 전자 메일에 직접 붙여서 원하는대로 렌더링되는지 확인하십시오. 그렇지 않다면 작동 할 때까지 조정 한 다음 * *를 생성하십시오. 만약 그렇다면, 분명히'sendEmailDto'는 당신이 기대했던대로 원시 문자열을 전달하지 않습니다. –

답변

2

헤더에이 나쁜 소년을 원합니다. 그렇지 않으면 HTML 형식이 될 것이라는 것을 모릅니다.

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

+1

헤더를 완전히 편집하고 제거 했으므로 올바르게 설정 되었습니까? –

+0

나는 또한 'Content-type : text/html; charset = iso-8859-1 '내 코드에서 8 행 – 1355

+0

그것은 이메일 헤더가 아니라 HTML 안에'' 태그입니다. 이메일 헤더는 내용이 HTML임을 지정해야하며, 그렇지 않으면 수신 측에서 형식이 지정되지 않습니다. –