ITPub博客

首页 > Linux操作系统 > Linux操作系统 > javamail中读数据出现乱码

javamail中读数据出现乱码

原创 Linux操作系统 作者:234390216 时间:2010-10-08 21:31:11 0 删除 编辑

下面是我的源代码:

我的源代码:
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;iBodyPart 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 s = 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 s = 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 s = 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");
}
}
}
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24006215/viewspace-675571/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-05-30

  • 博文量
    5
  • 访问量
    6338