我有以下程序,它充当使用POP3协议配置的邮箱侦听器,当程序执行时,它连接到邮箱并下载指定文件夹中的所有邮件和附件。
但现在我的查询是,现在邮件正文作为单独的文本文件下载,邮件附件也单独下载,我看的是当我的程序执行时,应该以Outlook消息格式下载该邮件当您在Outlook中打开邮件并想要将其保存在文件夹中时,您选择另存为选项,它会询问您要保存邮件的格式,然后选择Outlook邮件格式选项,然后保存该邮件在Outlook消息格式本身,所以请建议以Outlook消息格式保存邮件我需要在以下程序中进行哪些更改
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
public class ReceiveMailPOP3 {
private static final String HOST = "pop.gmail.com";
private static final String USERNAME = "myemail@gmail.com";
private static final String PASSWORD = "******";
public static void doit() throws MessagingException, IOException {
Folder folder = null;
Store store = null;
try {
Properties props = new Properties();
props.put("mail.store.protocol", "pop3s"); // Google uses POP3S not POP3
Session session = Session.getDefaultInstance(props);
// session.setDebug(true);
store = session.getStore();
store.connect(HOST, USERNAME, PASSWORD);
folder = store.getDefaultFolder().getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
System.out.println("No of Messages : " + folder.getMessageCount());
System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
for (int i=0; i < messages.length; ++i) {
System.out.println("MESSAGE #" + (i + 1) + ":");
Message msg = messages[i];
String from = "unknown";
if (msg.getReplyTo().length >= 1) {
from = msg.getReplyTo()[0].toString();
}
else if (msg.getFrom().length >= 1) {
from = msg.getFrom()[0].toString();
}
String subject = msg.getSubject();
System.out.println("Saving ... " + subject +" " + from);
// you may want to replace the spaces with "_"
// the files will be saved into the TEMP directory
String filename = "c:/temp/" + subject;
saveParts(msg.getContent(), filename);
}
}
finally {
if (folder != null) { folder.close(true); }
if (store != null) { store.close(); }
}
}
public static void saveParts(Object content, String filename)
throws IOException, MessagingException
{
OutputStream out = null;
InputStream in = null;
try {
if (content instanceof Multipart) {
Multipart multi = ((Multipart)content);
int parts = multi.getCount();
for (int j=0; j < parts; ++j) {
MimeBodyPart part = (MimeBodyPart)multi.getBodyPart(j);
if (part.getContent() instanceof Multipart) {
// part-within-a-part, do some recursion...
saveParts(part.getContent(), filename);
}
else {
String extension = "";
if (part.isMimeType("text/html")) {
extension = "html";
}
else {
if (part.isMimeType("text/plain")) {
extension = "txt";
}
else {
// Try to get the name of the attachment
extension = part.getDataHandler().getName();
}
filename = filename + "." + extension;
System.out.println("... " + filename);
out = new FileOutputStream(new File(filename));
in = part.getInputStream();
int k;
while ((k = in.read()) != -1) {
out.write(k);
}
}
}
}
}
}
finally {
if (in != null) { in.close(); }
if (out != null) { out.flush(); out.close(); }
}
}
public static void main(String args[]) throws Exception {
ReceiveMailPOP3.doit();
}
}
答案 0 :(得分:3)
在您保存邮件后,您希望能够对邮件做什么?
您可以将消息保存在&#34; .eml&#34; Outlook可以打开的文件。使用Message.writeTo方法。