首先分享一段关于JAVA MAIL的代码,
http://blog.csdn.net/aptweasel/archive/2007/03/09/1524872.aspx
以上是我收发邮件的代码,大家可以收下来自己测试,
在我的这个BLOG里还有其它相关代码,有兴趣的可以看一下,通过mail.163.com接收邮件,你们可以看出来,当你们发的邮件是GB2312,一般汉字都能正确写出来,如果你们客户端发邮件(如outlook,foxmail,可以自己设定字符集类型),字符集可能为UTF-8,GBK,这里你收到的文件就乱七八糟了,令我惊呀的是,在163里不会出现乱码,而我的客户端已经面目全非了。提示,(1)一般邮件都经过64位加密,在上面的URL里,提供了解密方法,可以直接使用,但只能解GB2312,其它的方法我也没有找到,才在网上求助,希望得到高人指点。
(2),字符集是由发送方决定的。
   我已经解决gbk,和gb2312,其实他们的编码基本一样,GBK是GB2312的扩展集,substring() 一下,去除乱码,得到的就得到结果了。
而UTF—8用了双字节编码,不一样了。怎么样才能得到它的结果
为了方便,我先写一个小程序。大家帮测试,public class Tools {
/*
 * 字符转换 */
public static String convertCode(String s) {
try {
//byte[] b = s.getBytes("ISO-8859-1");
//byte[] b = s.getBytes("UTF-8");
s = new String(b, "GBK");
} catch (IOException e) {
e.printStackTrace();
return "Exception";
}
return s;
}
/*
 * 64位解密算法
 */
public static String base64Decoder(String s) throws Exception {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] b = decoder.decodeBuffer(s);
return (new String(b));
}
public static void main(String args[]) {
String str = "";//以下字符串全是汉字“测试”
//str="=?gb2312?B?suLK1A==?=";
//str="=?utf-8?B?wqDmtYvor5U=?=";
//str="=?GBK?B?suLK1A==?=";
str="wqDmtYvor5U=";
// String str="suLK1LLiytQ"; try {
//str = convertCode(str);
System.out.println("st:" + str);
str = base64Decoder(str);
System.out.println("st:" + str);
} catch (Exception e) {
e.printStackTrace();
}

}
}

解决方案 »

  1.   

    收邮件很容易吧? 看看POP3就得了,。
      

  2.   

    一般情况下这个时候,只能在自己的程序里面对各种可能出现的字符集分开处理,可能工作量会非常大,而且非常的麻烦。我就处理过:
    utf-8
    gb2312
    iso-8859-1
    windows-31j
    shift_jis
    等等
      

  3.   

    问一下 .在你分享里的发邮件的代码
     package doudou;
     import javax.mail.*;
     import java.util.*;
     import javax.mail.internet.*; /**
     * @author Bromon
     */
     public class SenderWithSMTPVer
     {
     String host="";
     String user="";
     String password=""; public void setHost(String host)
     {
      this.host=host;
     } public void setAccount(String user,String password)
     {
      this.user=user;
      this.password=password;
     } public void send(String from,String to,String subject,String content)
     {
      Properties props = new Properties();
      props.put("mail.smtp.host", host);//指定SMTP服务器
      props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
      try
      {
       Session mailSession = Session.getDefaultInstance(props);
       
       mailSession.setDebug(true);//是否在控制台显示debug信息
       
       Message message=new MimeMessage(mailSession);
       message.setFrom(new InternetAddress(from));//发件人
       message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//收件人
       
       message.setSubject(subject);//邮件主题
       message.setText(content);//邮件内容
       message.saveChanges();
       
       Transport transport = mailSession.getTransport("smtp");
       transport.connect(host, user, password);
       transport.sendMessage(message, message.getAllRecipients());
       transport.close();
      }catch(Exception e)
      {
       System.out.println(e);
      }
      
     } public static void main(String args[])
     {
      SenderWithSMTPVer sm=new SenderWithSMTPVer();  sm.setHost("smtp.tom.com");//指定要使用的邮件服务器
      sm.setAccount("morningq","******");//指定帐号和密码  /*
     * @param String 发件人的地址
       * @param String 收件人地址
       * @param String 邮件标题
       * @param String 邮件正文
      */
      sm.send("[email protected]","[email protected]","标题","内容");
     } } 
    编译没问题!
    但是执行时报错如下:Exception in thread "main" java.lang.NoClassDefFoundError:SenderWithSMTPVer(wrong name:doudou/SenderWithSMTPVer)是虾米意思哦??
      

  4.   

    基本的HelloWorld是可以执行成功的.应该不是路径设置的问题!