使用javamail发送HTML格式邮件时用什么编码?

飞高乌鸦 发布于 2010/12/27 20:02
阅读 1K+
收藏 1

大虾们:

 

请问下使用javamail发送HTML格式邮件时用什么编码?

 

现在的情况:

1:使用smtp

2: 可以发送一个简单的带有链接的如:

SendMail.sendHtml("test@123.com", "试一下了", "这样可以吗?<a href='http://www.xxxx.com/'>xxx.com</a>");

3: 复杂的就发不了

   StringBuilder content = new StringBuilder();
   content.append("你好:<br/>其它内容测试");
   content.append("<p>访问地址:<a href='");
   content.append(Url); 
   content.append("'>");
   content.append(Url);  
   content.append("</a></p>");
   content.append("<br/></br/>谢谢!");
   System.out.println(content.toString());
   String c = URLEncoder.encode(content.toString(), "utf-8");

 

直接: content.toString的发不了
URLEncoder.encode的内容就全是%%%%%

怎么办?

 

加载中
0
os6101
os6101

我做的是在helper里边设置,要跟你项目的编码一致,例:

MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,"utf-8");

0
Kevin19701
Kevin19701

咋和偶做滴天差地别捏?

String s1 = "<!DOCTYPE html><html><body>";
        String s2 = "<table border='0' cellpadding='2' cellspacing='0' topmargin='0' leftmargin='0' marginwidth='0' marginheight='0'>";
        String s3 = "<tr><td bgcolor='#DDDDFF' align='center' width='85' height='12'><b>用户名</b></td><td bgcolor='#FFFFFF' align='center' width='1' height='12'></td><td bgcolor='#DDDDFF' align='center' width='85' height='12'><b>密码</b></td></tr>";
        String s4 = "<tr><td bgcolor='#FFFFFF' colspan='3' height='1'></td></tr>";
        String s5 = "<tr><td bgcolor='#DDDDFF' align='center' width='85' height='12'>" + myUsername + "</td><td bgcolor='#FFFFFF' align='center' width='1' height='12'></td><td bgcolor='#DDDDFF' align='center' width='85' height='12'>" + myPassword + "</td></tr>";
        String s6 = "</table></body></html>";
        String contentText = s1 + s2 + s3 + s4 + s5 + s6;

......

try
        {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            message.setRecipients(Message.RecipientType.TO, address);
            message.setSubject("Subject: my subject...");
            message.setSentDate(new Date());
            //message.setText(contentText);
            message.setContent(contentText, "text/html;charset=UTF-8");
            Transport.send(message);
            System.out.println("Java Mail Done...");
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }       
......

返回顶部
顶部