发送邮件后.收到的是乱码.大家帮我一下.怎么解决?下面是代码..在线等.. 不行...老大.你们有没有做过类似的东西.就是在线发送邮件的.然后可以发送中文的就行了..如果可以给我提供一个吗?先谢谢了..E-mail:[email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package com.netease.util;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class MailSender { static Log log = LogFactory.getLog(MailSender.class); private Properties props; //系统属性 private String hostName; //邮件服务器名 private Session session; //邮件会话对象 private String userName; //smtp认证用戶名 private String password; //smtp认证密码 private boolean isNeedAuth; //是否进行smtp身份认证标志 private MimeMessage mimeMsg; //MIME邮件消息对象 private Multipart mp; //Multipart对象,邮件內容,标题,附件等內容均添加到其中后再生成MimeMessage物件 public MailSender() { hostName = "hostName"; //指定默认的邮件服务器 userName = "userName"; password = "password"; isNeedAuth = true; init(); } public MailSender( String hostName, String userName, String password, boolean isNeedAuth) { this.hostName = hostName; this.userName = userName; this.password = password; this.isNeedAuth = isNeedAuth; init(); } /** * @return boolean */ private void init() { // 初始化系统属性 props = System.getProperties(); props.put("mail.smtp.host", hostName); if (isNeedAuth) props.put("mail.smtp.auth", "true"); else props.put("mail.smtp.auth", "false"); // 初始化邮件会话 session = Session.getDefaultInstance(props, null); // 初始化MIME邮件消息 mimeMsg = new MimeMessage(session); // 初始化邮件Multipart对象 mp = new MimeMultipart(); } /** * 设置发信人 * @param name String * @param pass String */ public void setFrom(String from) throws MailException { if (from == null || from.length() <= 0) throw new MailException("Form is empty."); log.debug("from: " + from); try { mimeMsg.setFrom(new InternetAddress(from)); } catch (MessagingException e) { throw new MailException("设置发信人:" + from + " 发生错误!" + e); } } /** * 设置接收人 * @param name String * @param pass String */ public void setTo(String to) throws MailException { if (to == null || to.length() <= 0) throw new MailException("To is empty."); log.debug("to: " + to); try { mimeMsg.setRecipients( Message.RecipientType.TO, InternetAddress.parse(to)); } catch (MessagingException e) { throw new MailException("设置接收人: " + to + " 发生错误!", e); } } /** * 设置抄送人 * @param name String * @param pass String */ public void setCopyTo(String copyTo) throws MailException { if (copyTo == null || copyTo.length() <= 0) return; log.debug("copyTo: " + copyTo); try { mimeMsg.setRecipients( Message.RecipientType.CC, InternetAddress.parse(copyTo)); } catch (MessagingException e) { throw new MailException("设置抄送人: " + copyTo + " 发生错误!", e); } } /** * 设置邮件主题 * @param mailSubject String * @return boolean */ public void setSubject(String mailSubject, String charset) throws MailException { log.debug("mailSubject: " + mailSubject + "charset: " + charset); try { mimeMsg.setSubject(mailSubject, charset); } catch (MessagingException e) { throw new MailException("设置邮件主题发生错误!", e); } } /** * 设置邮件正文 * @param mailBody String */ public void setBody(String mailBody, String type) throws MailException { log.debug("mailBody: " + mailBody + "type: " + type); try { BodyPart bp = new MimeBodyPart(); bp.setContent(mailBody, type); mp.addBodyPart(bp); } catch (MessagingException e) { throw new MailException("设置邮件正文时发生错误!" + e); } } /** * 添加邮件附件 * @param name String * @param pass String */ public void addFileAffix(String filename) throws MailException { if (filename == null || filename.length() <= 0) return; try { BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp.setFileName(fileds.getName()); bp.setDataHandler(new DataHandler(fileds)); mp.addBodyPart(bp); } catch (MessagingException e) { throw new MailException("添加邮件附件:" + filename + "发生错误!" + e); } } /** * @param name String * @param pass String */ public void send() throws MailException { try { mimeMsg.setContent(mp); mimeMsg.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect( (String) props.get("mail.smtp.host"), userName, password); transport.sendMessage( mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO)); log.debug("邮件发送成功!"); transport.close(); } catch (MessagingException e) { throw new MailException("邮件发送失败!", e); } } /** * Just do it as this */ public static void main(String[] args) { String mailbody = "Test message!"; MailSender themail = new MailSender(); try { ms.setBody("body", "text/html;charset=GBK"); themail.setBody(mailbody, "text/html;charset=GBK"); themail.setTo("[email protected]"); themail.setFrom("[email protected]"); themail.addFileAffix("c:\xxx.txt"); themail.send(); } catch (MailException e) { e.printStackTrace(); } }} 主要是改变收到时的乱码.是要怎么解决文字.是在tomcat4.1下的 415.8%23.1为啥会计算错误 求解! java拆分日期 dom4j个别汉字不能转换问题? 基XML的新闻发布系统 java执行sql文件数据库恢复,求大神指导,实在找不出那里有问题 struts求助,望高手指点 急救: 请问怎样bind到jboss的Naming server spring+mybatis项目启动报错 刚刚安装了apache tomcat5.0.16 server, 为什么那些examples都可以用,而我自己写的jsp文件不能运行呢?(最简单的HELLO.jsp) 请教高手一个关于weblogic的问题。。。。。。在线等待。。。。。。先谢了。。。。
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;public class MailSender { static Log log = LogFactory.getLog(MailSender.class); private Properties props; //系统属性
private String hostName; //邮件服务器名
private Session session; //邮件会话对象 private String userName; //smtp认证用戶名
private String password; //smtp认证密码
private boolean isNeedAuth; //是否进行smtp身份认证标志 private MimeMessage mimeMsg; //MIME邮件消息对象 private Multipart mp; //Multipart对象,邮件內容,标题,附件等內容均添加到其中后再生成MimeMessage物件 public MailSender() {
hostName = "hostName"; //指定默认的邮件服务器
userName = "userName";
password = "password";
isNeedAuth = true;
init();
} public MailSender(
String hostName,
String userName,
String password,
boolean isNeedAuth) {
this.hostName = hostName;
this.userName = userName;
this.password = password;
this.isNeedAuth = isNeedAuth;
init();
} /**
* @return boolean
*/
private void init() {
// 初始化系统属性
props = System.getProperties();
props.put("mail.smtp.host", hostName);
if (isNeedAuth)
props.put("mail.smtp.auth", "true");
else
props.put("mail.smtp.auth", "false");
// 初始化邮件会话
session = Session.getDefaultInstance(props, null);
// 初始化MIME邮件消息
mimeMsg = new MimeMessage(session);
// 初始化邮件Multipart对象
mp = new MimeMultipart();
} /**
* 设置发信人
* @param name String
* @param pass String
*/
public void setFrom(String from) throws MailException {
if (from == null || from.length() <= 0)
throw new MailException("Form is empty."); log.debug("from: " + from);
try {
mimeMsg.setFrom(new InternetAddress(from));
} catch (MessagingException e) {
throw new MailException("设置发信人:" + from + " 发生错误!" + e);
}
} /**
* 设置接收人
* @param name String
* @param pass String
*/
public void setTo(String to) throws MailException {
if (to == null || to.length() <= 0)
throw new MailException("To is empty."); log.debug("to: " + to);
try {
mimeMsg.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(to));
} catch (MessagingException e) {
throw new MailException("设置接收人: " + to + " 发生错误!", e);
}
} /**
* 设置抄送人
* @param name String
* @param pass String
*/
public void setCopyTo(String copyTo) throws MailException {
if (copyTo == null || copyTo.length() <= 0)
return; log.debug("copyTo: " + copyTo);
try {
mimeMsg.setRecipients(
Message.RecipientType.CC,
InternetAddress.parse(copyTo));
} catch (MessagingException e) {
throw new MailException("设置抄送人: " + copyTo + " 发生错误!", e);
}
} /**
* 设置邮件主题
* @param mailSubject String
* @return boolean
*/
public void setSubject(String mailSubject, String charset)
throws MailException {
log.debug("mailSubject: " + mailSubject + "charset: " + charset);
try {
mimeMsg.setSubject(mailSubject, charset);
} catch (MessagingException e) {
throw new MailException("设置邮件主题发生错误!", e);
}
} /**
* 设置邮件正文
* @param mailBody String
*/
public void setBody(String mailBody, String type) throws MailException {
log.debug("mailBody: " + mailBody + "type: " + type);
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(mailBody, type);
mp.addBodyPart(bp);
} catch (MessagingException e) {
throw new MailException("设置邮件正文时发生错误!" + e);
}
} /**
* 添加邮件附件
* @param name String
* @param pass String
*/
public void addFileAffix(String filename) throws MailException {
if (filename == null || filename.length() <= 0)
return; try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setFileName(fileds.getName());
bp.setDataHandler(new DataHandler(fileds));
mp.addBodyPart(bp);
} catch (MessagingException e) {
throw new MailException("添加邮件附件:" + filename + "发生错误!" + e);
}
} /**
* @param name String
* @param pass String
*/
public void send() throws MailException {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges(); Transport transport = session.getTransport("smtp");
transport.connect(
(String) props.get("mail.smtp.host"),
userName,
password);
transport.sendMessage(
mimeMsg,
mimeMsg.getRecipients(Message.RecipientType.TO)); log.debug("邮件发送成功!");
transport.close();
} catch (MessagingException e) {
throw new MailException("邮件发送失败!", e);
}
} /**
* Just do it as this
*/
public static void main(String[] args) { String mailbody = "Test message!"; MailSender themail = new MailSender(); try {
ms.setBody("body", "text/html;charset=GBK");
themail.setBody(mailbody, "text/html;charset=GBK");
themail.setTo("[email protected]");
themail.setFrom("[email protected]");
themail.addFileAffix("c:\xxx.txt");
themail.send();
} catch (MailException e) {
e.printStackTrace();
}
}
}