public Message[] getMail(String host, String name, String password) throws Exception {
Properties prop = new Properties();
prop.put("mail.pop3.host", host);
Session session = Session.getDefaultInstance(prop);
store = session.getStore("pop3");
store.connect(host, name, password); inbox = store.getDefaultFolder().getFolder("INBOX");
//inbox.open(Folder.READ_ONLY);
inbox.open(Folder.READ_WRITE); Message[] msg = inbox.getMessages(); FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(msg, profile); return msg;
} public int handleMessage(Message msg) throws Exception {
String disposition;
BodyPart part; //得到附件为.eml的数目
int count = 0;
//得到用户ID
String senderID; Multipart mp = (Multipart)msg.getContent();
int mpCount = mp.getCount();
for (int m = 0; m < mpCount; m++) {
part = mp.getBodyPart(m);
disposition = part.getDisposition();
if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
//有附件
String temp = part.getFileName(); //得到未经处理的附件名字
String fileName = temp; //用来记录附件名称(有可能需要base64解码)
//先判断是否需要进行base64编码
if (temp.toLowerCase().indexOf("=?gb2312?b?") != -1) {
String s = temp.substring(11, temp.indexOf("?=") - 1); //去到header和footer //文件名一般都经过了base64编码,下面是解码
fileName = base64Decoder(s);
}
System.out.println("有附件:" + fileName);
count++;
saveAttachment(fileName, part);
}
} return count;
} private void saveAttachment(String fileName, BodyPart part) throws Exception {
String directory = Share.emailDirectory; String filePath = directory + "\\" + fileName;
int count = 1;
tempFile = new File(filePath); //开始保存附件
InputStream in = part.getInputStream();
FileOutputStream writer = new FileOutputStream(tempFile);
byte[] content = new byte[255];
int read = 0;
while ((read = in.read(content)) != -1) {
writer.write(content);
}
writer.close();
in.close();
}先是调用Message[] getMail()来接收邮件,然后对每一封接收的邮件进行handleMessage(Message msg)处理,如果有附件,则调用saveAttachment(String fileName, BodyPart part)来保存附件.
但现在表面上正常,但问题是, 在保存附件时,会在原附件内容的基础上多写了一段信息. 比如,邮件的附件是一个.txt文件,通过上面程序保存得到的txt文件的内容与发送方发送的不一样, 其内容是原txt内容的基础上,添加了一大串空格符. 原本的txt文件只有61字节,但用上面程序得到的.txt文件却有255字节.不知道问题出在什么地方, 大人们急救啊, 或是给其他接收邮件并保存附件的方法也行, 小弟不甚感激!
Properties prop = new Properties();
prop.put("mail.pop3.host", host);
Session session = Session.getDefaultInstance(prop);
store = session.getStore("pop3");
store.connect(host, name, password); inbox = store.getDefaultFolder().getFolder("INBOX");
//inbox.open(Folder.READ_ONLY);
inbox.open(Folder.READ_WRITE); Message[] msg = inbox.getMessages(); FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(msg, profile); return msg;
} public int handleMessage(Message msg) throws Exception {
String disposition;
BodyPart part; //得到附件为.eml的数目
int count = 0;
//得到用户ID
String senderID; Multipart mp = (Multipart)msg.getContent();
int mpCount = mp.getCount();
for (int m = 0; m < mpCount; m++) {
part = mp.getBodyPart(m);
disposition = part.getDisposition();
if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
//有附件
String temp = part.getFileName(); //得到未经处理的附件名字
String fileName = temp; //用来记录附件名称(有可能需要base64解码)
//先判断是否需要进行base64编码
if (temp.toLowerCase().indexOf("=?gb2312?b?") != -1) {
String s = temp.substring(11, temp.indexOf("?=") - 1); //去到header和footer //文件名一般都经过了base64编码,下面是解码
fileName = base64Decoder(s);
}
System.out.println("有附件:" + fileName);
count++;
saveAttachment(fileName, part);
}
} return count;
} private void saveAttachment(String fileName, BodyPart part) throws Exception {
String directory = Share.emailDirectory; String filePath = directory + "\\" + fileName;
int count = 1;
tempFile = new File(filePath); //开始保存附件
InputStream in = part.getInputStream();
FileOutputStream writer = new FileOutputStream(tempFile);
byte[] content = new byte[255];
int read = 0;
while ((read = in.read(content)) != -1) {
writer.write(content);
}
writer.close();
in.close();
}先是调用Message[] getMail()来接收邮件,然后对每一封接收的邮件进行handleMessage(Message msg)处理,如果有附件,则调用saveAttachment(String fileName, BodyPart part)来保存附件.
但现在表面上正常,但问题是, 在保存附件时,会在原附件内容的基础上多写了一段信息. 比如,邮件的附件是一个.txt文件,通过上面程序保存得到的txt文件的内容与发送方发送的不一样, 其内容是原txt内容的基础上,添加了一大串空格符. 原本的txt文件只有61字节,但用上面程序得到的.txt文件却有255字节.不知道问题出在什么地方, 大人们急救啊, 或是给其他接收邮件并保存附件的方法也行, 小弟不甚感激!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货