我有几天试图通过 Google App Engine 项目上的 IMAP 获取消息的内容。
我设法提取了所有其他信息,但是提取的内容让我跳出了一条错误消息(即使调用 message.getContent.tostring() 也不起作用,我已经尝试过作为 MultiPart)。
我从普通项目(不是 GAE 并使用 javamail.1.4.7)执行相同的操作,消息的内容完美显示。
这是GAE项目的代码:
import java.util.Properties;
import java.util.logging.Logger;
import javax.mail.Session;
import java.io.IOException;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Store;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class nuevo extends HttpServlet {
private String User;
private String Pass;
private static final Logger log = Logger.getLogger(nuevo.class
.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
User = "User";
Pass = "Pass";
LlamaIMAP(resp);
}
public void LlamaIMAP(HttpServletResponse resp) {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.put("mail.imap.host", "imap.gmail.com");
props.put("mail.imap.user", User);
props.put("mail.imap.socketFactory", 993);
props.put("mail.imap.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.imap.port", 993);
Session session = Session.getDefaultInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(User, Pass);
}
});
try {
Store store = session.getStore("imap");
store.connect("imap.gmail.com", 993, User, Pass);
Folder fldr = store.getFolder("Inbox");
fldr.open(Folder.HOLDS_MESSAGES);
// HOLDS_MESSAGES);
Message[] ar = fldr.getMessages();
int count = fldr.getMessageCount();
resp.getWriter().println(count);
resp.getWriter().println(ar[0].getAllRecipients()[0].toString());
resp.getWriter().println(ar[0].getFrom()[0].toString());
resp.getWriter().println(ar[0].getSentDate().toString());
resp.getWriter().println(ar[0].getSubject());
resp.getWriter().println(ar[0].getContent().toString());
} catch (Exception exc) {
try {
resp.getWriter().println(exc + "error");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}