请高人指点,java收邮件,急!急!急!

aptweasel 2007-03-09 04:09:25
首先分享一段关于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();
}

}
}
...全文
689 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzheng2001 2007-04-29
  • 打赏
  • 举报
回复
最近在做邮件程序,有时间帮你看一下.
luyang1016 2007-04-28
  • 打赏
  • 举报
回复
一般情况下这个时候,只能在自己的程序里面对各种可能出现的字符集分开处理,可能工作量会非常大,而且非常的麻烦。

我就处理过:
utf-8
gb2312
iso-8859-1
windows-31j
shift_jis
等等
prettyj 2007-04-28
  • 打赏
  • 举报
回复
基本的HelloWorld是可以执行成功的.应该不是路径设置的问题!
prettyj 2007-04-28
  • 打赏
  • 举报
回复
能帮楼上的解答一下么
prettyj 2007-04-28
  • 打赏
  • 举报
回复
问一下 .在你分享里的发邮件的代码
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("morningq@tom.com","zeng@tom.com","标题","内容");
}

}
编译没问题!
但是执行时报错如下:Exception in thread "main" java.lang.NoClassDefFoundError:SenderWithSMTPVer(wrong name:doudou/SenderWithSMTPVer)是虾米意思哦??
WIN_ANGEL 2007-04-25
  • 打赏
  • 举报
回复
帮顶关注中
w_yuxiang 2007-03-12
  • 打赏
  • 举报
回复
帮顶
yeah920 2007-03-12
  • 打赏
  • 举报
回复
帮顶,学习了。
aptweasel 2007-03-12
  • 打赏
  • 举报
回复
收邮件是很容易,可是对于不同服务器发来的邮件,它的编码集可能不一样,要是能识别所有的字符集就不容易了。
xk2y 2007-03-09
  • 打赏
  • 举报
回复
学习了``````
healer_kx 2007-03-09
  • 打赏
  • 举报
回复
收邮件很容易吧? 看看POP3就得了,。
Red_angelX 2007-03-09
  • 打赏
  • 举报
回复
谢谢共享 接分

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧