Jsp乱码的问题 在JSP开发中,在网页上可以显示中文,但在用邮件发送后, 到 邮件接收人那里出来 的乱码,请问各位大侠如何解决 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在接收页面开始加一句request.setCharEncoding("gb2312");试看看 应该是编码的问题,如果邮件内容是unicode字符,对方必须给自己的浏览器设置为utf-8编码 public void postMail(String to,String sb,String con) throws MessagingException,Exception { String[] recipients = new String[1]; recipients[0] = to; if (recipients != null && recipients.length > 0) { /*String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); PopupAuthenticator pauth = new PopupAuthenticator("[email protected]", "313131"); java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); Session session = Session.getInstance(props,pauth); Message msg = new MimeMessage(session); */ String contentType = "html"; Properties props = new Properties(); props.put("mail.smtp.host","smtp.163.com"); props.put("mail.smtp.auth","true"); props.put("mail.smtp.user", "[email protected]"); props.put("mail.smtp.password", "61664774"); Session session = Session.getInstance(props, new PopupAuthenticator("[email protected]", "61664774")); Message msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress("[email protected]");msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // 设置邮件标题,中文编码 sb = MimeUtility.encodeText(sb, "GBK", "B"); msg.setSubject(sb); if (contentType == null || contentType.equals("text")) { msg.setText(con); } else if (contentType.equals("html")) { BodyPart bodyPart1 = new MimeBodyPart(); bodyPart1.setContent(con, "text/html;charset=gbk"); Multipart mmp = new MimeMultipart(); mmp.addBodyPart(bodyPart1); msg.setContent(mmp); } msg.setSentDate(new java.util.Date()); Transport.send(msg); } } 这个是我发邮件的一个方法。注释部分是采用gmail服务器 求助,flex4中DataGrid修改值问题 tomcat连接池问题,在线请高手指点 如何引用JSP页面 如何获得post过来的原始数据 java获取文件路径的问题 在java下在常用的AJAX有哪些 关于Session生命周期的问题 jsp权限问题 php程序和jsp程序能同时部署到一个网站上面吗? 急急急?怎么样在IE中能实现象数据窗口那样的排序!! 求教一个tomcat与sqlserver连接的问题 如何实现?
试看看
throws MessagingException,Exception
{
String[] recipients = new String[1];
recipients[0] = to;
if (recipients != null && recipients.length > 0)
{
/*String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
PopupAuthenticator pauth = new PopupAuthenticator("[email protected]", "313131");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Session session = Session.getInstance(props,pauth);
Message msg = new MimeMessage(session);
*/
String contentType = "html";
Properties props = new Properties();
props.put("mail.smtp.host","smtp.163.com");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.user", "[email protected]");
props.put("mail.smtp.password", "61664774");
Session session = Session.getInstance(props, new PopupAuthenticator("[email protected]", "61664774"));
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress("[email protected]");msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// 设置邮件标题,中文编码
sb = MimeUtility.encodeText(sb, "GBK", "B");
msg.setSubject(sb);
if (contentType == null || contentType.equals("text"))
{
msg.setText(con);
}
else if (contentType.equals("html"))
{
BodyPart bodyPart1 = new MimeBodyPart();
bodyPart1.setContent(con, "text/html;charset=gbk");
Multipart mmp = new MimeMultipart();
mmp.addBodyPart(bodyPart1);
msg.setContent(mmp);
}
msg.setSentDate(new java.util.Date());
Transport.send(msg);
}
}