javamail接收邮件,解析时乱码,想转回去怎么转呢

Elim-Zhang 2010-10-07 11:32:55
我的源代码:
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeUtility;

public class RecevieMailDemo3 {

public static void main(String args[]) throws Exception {
Properties props = System.getProperties();

Session session = Session.getDefaultInstance(props);

Store store = session.getStore("pop3");
store.connect("pop3.sohu.com", "xxxxxxxxxxx", "xxxxxxxxxxxx");

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);

Message[] messages = folder.getMessages();

for (Message msg:messages) {
String contentType = msg.getContentType();

if (msg.isMimeType("text/plain")) {
System.out.println(msg.getContent());

} else if (msg.isMimeType("multipart/mixed")){
Multipart multipart = (Multipart)msg.getContent();
int count = multipart.getCount();
// System.out.println(count);
for (int i=0;i<count;i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String disposition = bodyPart.getDisposition();
System.out.println(bodyPart.getContentType()+i);
if (disposition !=null && disposition.equals(Part.ATTACHMENT)) {
System.out.println("ATTACHMENT");
DataHandler dataHandler = bodyPart.getDataHandler();
String name = MimeUtility.decodeText(dataHandler.getName());
InputStream is = dataHandler.getInputStream();
OutputStream os = new FileOutputStream("D:/temp/"+name);
byte[] bytes = new byte[1024];
while (is.read(bytes)!=-1) {
os.write(bytes);
}
System.out.println(name+i);
} else if (bodyPart.isMimeType("multipart/related")){
System.out.println(((Multipart)bodyPart.getContent()).getBodyPart(0).getContent()+"***content***"+i);
InputStream is = bodyPart.getInputStream();
OutputStream os = new FileOutputStream("d:/temp/demo3.html");
byte []bytes = new byte[1024];
while(is.read(bytes)!=-1) {
os.write(bytes);
}
os.flush();
is.close();
os.close();
} }
} else if (msg.isMimeType("text/html")) {
InputStream is = msg.getInputStream();
OutputStream os = new FileOutputStream("d:/temp/demo3.mht");
byte b[] = new byte[1024];
while (is.read(b) != -1) {
os.write(b);
}
os.flush();
is.close();
os.close();
System.out.println("HTMLHTMLHTMLHTMLHTMLHTMLHTMLHTML");
}
}
}
}




写入html的邮件的内容:------=_=BF=A7=E0=AC=BB=CA=B9=AC=B6=ED=BA=A3=B9=D8=C0=B1=BB=F0=B9=F8=B0=A2=C0=EF=B9=

=FD=BA=F3=DA=C0=D2=BB=B8=F6=C4=C7=B8=F6=C0=AD=B8=F1=BA=CD=B0=A2=B8=E7=B9=FE=

=B9=FE=B8=F6=C0=AC=BB=F8=B9=DC=C0=ED=B8=F6=C5=B6=CE=D2=D2=BB=B9=B2=D2=AA=B7= 就是类似于这样的BASE64编码,该怎样把base64编码转回去呢

...全文
110 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Elim-Zhang 2010-10-12
来人,帮帮忙啊
回复
Elim-Zhang 2010-10-09
谁来帮帮我!
回复
Elim-Zhang 2010-10-08
各位大虾们,帮帮忙吧!
回复
Elim-Zhang 2010-10-08
InputStream is = bodyPart.getInputStream();
OutputStream os = new FileOutputStream("d:/temp/demo3.html");
byte []bytes = new byte[1024];
while(is.read(bytes)!=-1) {
os.write(bytes);
}
就是这样把内容拿出来然后再放到另一个文件里面去的,放了之后就会出现上述的BASE64码
回复
soli11722984 2010-10-08
MimeUtility.decodeText(String etext)
MimeUtility.decodeWord(String eword)
回复
soli11722984 2010-10-08
MimeUtility.decode(InputStream is, String encoding)
回复
Elim-Zhang 2010-10-08
这帖子怎么没人关注呢
回复
Elim-Zhang 2010-10-08
还是不行啊,
回复
超级小胖 2010-10-08
正在研究当中..
回复
singlesteven 2010-10-08
你都知道是base64乱码,那对邮件获到的内容进行解码不就行了,

百度找个base64解码代码就Ok了
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-07 11:32
社区公告
暂无公告