在jsp中使用javamail总是出现中文乱码 怎么办

[code="jsp"]

<%@page import="java.util.*"%>

<%@page import="javax.mail.*"%>

<%@page import="javax.mail.internet.*"%>

<%@page import="javax.activation.*"%>



JSP meets JavaMail,what a sweet combo.




<%
try

{
String title = "wangke ke倪超";
String serversina = "smtp.sina.com";
// String serverSohu = "smtp.sohu.net";
Properties props = new Properties();

props.put("mail.smtp.host",serversina);//指定SMTP服务器

props.put("mail.smtp.auth","true");//指定是否需要SMTP验证
     Session mailSession = Session.getDefaultInstance(props);  

     mailSession.setDebug(true);//是否在控制台显示debug信息    
     Message message=new MimeMessage(mailSession);  
     message.setFrom(new InternetAddress("ttf56mail@sina.com"));//发件人  
     message.addRecipient(Message.RecipientType.TO,new InternetAddress("nichao1@sina.com"));//收件人     


     sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
     message.setSubject("=?GB2312?B?"+enc.encode(title.getBytes())+"?=");

     message.setText(title);//邮件内容  
     message.saveChanges();  
     Transport   transport =   mailSession.getTransport("smtp");  
     transport.connect(serversina,"ttf56mail","123456");    //这个邮箱可随便使用  
     transport.sendMessage(message,message.getAllRecipients());  
     transport.close();  

%>

Your mail has been sent.

<%

} catch(MessagingException m) {

out.println(m.toString());

} %>


[/code]

3个回答

先在JSP里面设置如下信息再试
[quote]<%@page contentType="text/html; charset=UTF-8"%>
[/quote]
.编码集请按需修改.

你jsp页面没设置页面编码啊,

[quote]在jsp中使用javamail总是出现中文乱码 怎么办[/quote]
页面要加你所以支持的字符集(UTF-8,GBK,GB2312)才行啊...
[quote]
<%@ page language="java" contentType="text/html; [color=red]charset=UTF-8[/color]"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">







....[/quote]
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐