1 当内容为html格式时,中文文字就是乱码了:不知道应该在哪里设置,我的代码如下:
public void sendHtml()
throws Exception
{
sun.misc.BASE64Encoder enc= new sun.misc.BASE64Encoder();
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
Authenticator auth = new MyAuthenticator(user, password);
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
if(bcc != null && !bcc.equals(""))
{
message.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(bcc));
}
message.setSubject(subject,"gb2312");
message.setDataHandler(new DataHandler(new StringDataSource(messageText, "text/html")));
Transport.send(message);
}
public void sendHtml()
throws Exception
{
sun.misc.BASE64Encoder enc= new sun.misc.BASE64Encoder();
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
Authenticator auth = new MyAuthenticator(user, password);
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
if(bcc != null && !bcc.equals(""))
{
message.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(bcc));
}
message.setSubject(subject,"gb2312");
message.setDataHandler(new DataHandler(new StringDataSource(messageText, "text/html")));
Transport.send(message);
}
解决方案 »
- java长传问题,输入输出流
- 我怎么对jsp表中的学生的成绩的排名啊
- JSP 连接 SQL2000 数据库的问题(续)
- JSP页面字符集的奇怪问题?
- 招集令
- 新手提问:jsp通过jdbc连接oracle通不过,请指点。
- 请问在jsp中怎样禁止用户用 浏览器 上的"后退"钮回到前面访问的页面!!谢谢大家!!
- 时间在数据库中的查询问题
- 用jrun4.0 如何发布站点?在线等待。马上揭贴
- 从bean中调用“中文字符串数组”却出现问题。
- spring使用监听器加载配置文件的问题
- struts菜鸟: JBuild2006 中struts小实例老是出现 Missing message for key "item.username"??错误
Session sendMailSession;
Transport transport;
MyAuthenticator myauth = new MyAuthenticator("邮件服务器用户名","邮件服务器密码");
sendMailSession = Session.getInstance(props, myauth);
props.put("mail.smtp.host",smtphost);
props.put("mail.smtp.auth","true"); //这样才能通过验证Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(content); //给消息对象设置内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(content,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
newMessage.setContent(mm);//把mm作为消息对象的内容
newMessage.saveChanges();
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
html中的编码描述为:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
但是你邮件编码使用另外一种编码,那就会出错