linux下用java mail发送邮件成功,但中文内容为乱码?急! key=new String(strKey.getBytes("UTF-8"),"GBK");或者内容做成htm格式,加个 sb.append(" <meta http-equiv='Content-Type' content='text/html; charset=GBK'>").append("\n"); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //设置邮件正文 public boolean setBody(String mailBody) { try { BodyPart bp = new MimeBodyPart(); bp.setContent( "<meta http-equiv=Content-Type content='text/html; charset=GBK'>" + mailBody, "text/html;charset=GBk"); mp.addBodyPart(bp); return true; } catch (Exception e) { e.printStackTrace(); return false; } } package mail;import java.util.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import java.io.*;public class SendMail { private MimeMessage mimeMsg; //MIME邮件对象 private Session session; //邮件会话对象 private Properties props; //系统属性 private String username = ""; //smtp认证用户名和密码 private String password = ""; private Multipart mp; //Multipart对象 public SendMail(String smtp) { setSmtpHost(smtp); createMimeMessage(); } //设置发送邮件服务器 public void setSmtpHost(String hostName) { System.out.println("设置系统属性:mail.smtp.host = " + hostName); if (props == null) props = System.getProperties(); //获得系统属性对象 props.put("mail.smtp.host", hostName); //设置SMTP主机 } //创建邮件session public boolean createMimeMessage() { try { session = Session.getDefaultInstance(props, null); //获得邮件会话对象 } catch (Exception e) { e.printStackTrace(); return false; } try { mimeMsg = new MimeMessage(session); //创建MIME邮件对象 mp = new MimeMultipart(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } //是否发邮件需要验证 public void setNeedAuth(boolean need) { if (props == null) props = System.getProperties(); if (need) { props.put("mail.smtp.auth", "true"); } else { props.put("mail.smtp.auth", "false"); } } //设置用户名 密码 public void setNamePass(String name, String pass) { username = name; password = pass; } //设置邮件主题 public boolean setSubject(String mailSubject) { System.out.println("设置邮件主题!"); try { mimeMsg.setSubject(mailSubject); return true; } catch (Exception e) { e.printStackTrace(); return false; } } //设置邮件正文 public boolean setBody(String mailBody) { try { BodyPart bp = new MimeBodyPart(); bp.setContent( "<meta http-equiv=Content-Type content='text/html; charset=GBK'>" + mailBody, "text/html;charset=GBk"); mp.addBodyPart(bp); return true; } catch (Exception e) { e.printStackTrace(); return false; } } //添加附件 public boolean addFileAffix(String filename) { System.out.println("增加邮件附件:" + filename); try { BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(fileds.getName()); mp.addBodyPart(bp); return true; } catch (Exception e) { e.printStackTrace(); return false; } } //设置发件人信息 public boolean setFrom(String from) { System.out.println("设置发信人!"); try { mimeMsg.setFrom(new InternetAddress(from)); //设置发信人 return true; } catch (Exception e) { return false; } } //发送地址 public boolean setTo(String to) { if (to == null) return false; try { mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); return true; } catch (Exception e) { return false; } } //抄送一个地址 public boolean setCopyTo(String copyto) { if (copyto == null) return false; try { mimeMsg.setRecipients(Message.RecipientType.CC, (Address[]) InternetAddress.parse(copyto)); return true; } catch (Exception e) { return false; } } //发送邮件 public boolean sendout() { try { mimeMsg.setContent(mp); mimeMsg.saveChanges(); System.out.println("正在发送邮件...."); Session mailSession = Session.getInstance(props, null); Transport transport = mailSession.getTransport("smtp"); transport.connect((String) props.get("mail.smtp.host"), username, password); transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType. TO)); System.out.println("发送邮件成功!"); transport.close(); return true; } catch (Exception e) { System.err.println("邮件发送失败!" + e); return false; } } /** * Just do it as this */ public static void main(String[] args) { String mailbody = "hello,你好吗????这是中文邮件测试"; SendMail themail = new SendMail("mail.tcaccp.com"); //设置smtp服务器 themail.setNeedAuth(true); //服务器需要身份验证 if (themail.setSubject("标题") == false) return; //设置标题 if (themail.setBody(mailbody) == false) return; //正文内容 if (themail.setTo("[email protected]") == false) return; //收件人 if (themail.setFrom("[email protected]") == false) return; //发件人 if (themail.addFileAffix("C:\\tmp\\a.rar") == false) return; //附件 请使用绝对地址 themail.setNamePass("[email protected]", "....."); //发件人的邮箱帐号,密码 if (themail.sendout() == false) return; }} 请问楼上,我测试了一下,你的程序邮件文本内容显示正常,但标题(subject)显示仍为乱码,请问是这样吗? 我在linux测试的,显示标题仍为乱码。 标题要注意编码,linux用什么编码的? 你的程序编码是什么?转下编码 key=new String(strKey.getBytes("GBK"),"UTF-8"); new String(strKey.getBytes("GBK"),"UTF-8"); 我试了,不行new String(strKey.getBytes("UTF-8"),"GBK"); 不行 请问swing 在1.6和1.5版本第区别 下班散分,顺便发个通用分页查询 大家帮忙参考一下这个单帮别人做多少钱合适?散分 hibernate 老说没有配方言 最新发现的JDK1.5的问题,很多 请帮我做论坛测试。(struts+hibernate完成) 使用org.apache.struts.upload.FormFile的问题?????? 可不可以将Struts的应用部署到resin下? Tomcat连接数据源问题!急求解! 寻求帮助,谢谢大家关注 困扰好久的问题:hibernate级联问题 谁能发一个struts+hibernate+spring的例子给我,非常感谢
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<meta http-equiv=Content-Type content='text/html; charset=GBK'>" +
mailBody, "text/html;charset=GBk");
mp.addBodyPart(bp); return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;public class SendMail { private MimeMessage mimeMsg; //MIME邮件对象
private Session session; //邮件会话对象
private Properties props; //系统属性
private String username = ""; //smtp认证用户名和密码
private String password = "";
private Multipart mp; //Multipart对象 public SendMail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
//设置发送邮件服务器
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties(); //获得系统属性对象 props.put("mail.smtp.host", hostName); //设置SMTP主机
}
//创建邮件session
public boolean createMimeMessage() {
try {
session = Session.getDefaultInstance(props, null); //获得邮件会话对象
} catch (Exception e) {
e.printStackTrace();
return false;
}
try {
mimeMsg = new MimeMessage(session); //创建MIME邮件对象
mp = new MimeMultipart(); return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//是否发邮件需要验证
public void setNeedAuth(boolean need) {
if (props == null)
props = System.getProperties(); if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
//设置用户名 密码
public void setNamePass(String name, String pass) {
username = name;
password = pass;
}
//设置邮件主题
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//设置邮件正文
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<meta http-equiv=Content-Type content='text/html; charset=GBK'>" +
mailBody, "text/html;charset=GBk");
mp.addBodyPart(bp); return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//添加附件
public boolean addFileAffix(String filename) { System.out.println("增加邮件附件:" + filename); try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName()); mp.addBodyPart(bp); return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//设置发件人信息
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
return true;
} catch (Exception e) {
return false;
}
}
//发送地址
public boolean setTo(String to) {
if (to == null)
return false; try {
mimeMsg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
return true;
} catch (Exception e) {
return false;
} } //抄送一个地址
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
//发送邮件
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg,
mimeMsg.getRecipients(Message.RecipientType.
TO)); System.out.println("发送邮件成功!");
transport.close(); return true;
} catch (Exception e) {
System.err.println("邮件发送失败!" + e);
return false;
}
}
/**
* Just do it as this
*/
public static void main(String[] args) {
String mailbody = "hello,你好吗????这是中文邮件测试";
SendMail themail = new SendMail("mail.tcaccp.com"); //设置smtp服务器
themail.setNeedAuth(true); //服务器需要身份验证 if (themail.setSubject("标题") == false)
return; //设置标题
if (themail.setBody(mailbody) == false)
return; //正文内容
if (themail.setTo("[email protected]") == false)
return; //收件人
if (themail.setFrom("[email protected]") == false)
return; //发件人
if (themail.addFileAffix("C:\\tmp\\a.rar") == false)
return; //附件 请使用绝对地址
themail.setNamePass("[email protected]", "....."); //发件人的邮箱帐号,密码 if (themail.sendout() == false)
return;
}
}
new String(strKey.getBytes("UTF-8"),"GBK"); 不行