如何获取gwt中的电子邮件内容?

时间:2011-12-02 08:26:59

标签: java google-app-engine

我在服务器上通过gwt rpc运行以下代码:

public class MailHandlerServlet  extends HttpServlet{

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    doPost(req, resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    try {
        MimeMessage message = new MimeMessage(session, req.getInputStream());
} catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我不知道解析MimeMessage的函数。如何获取MimeMessage的内容?

这里是我编写的简单代码,但它不起作用:

Object content = message.getContent();
if(content instanceof Multipart){
             Multipart mp = (Multipart)content;
             int count3 = mp.getCount();
             for(int i = 0;i < count3;i++){
                BodyPart p = mp.getBodyPart(i);
                if(p.isMimeType("text/plain")){ 
                    TextBody data = (TextBody) p.getContent();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    data.writeTo(baos);
                    String datafull = new String(baos.toByteArray());  
                    PrintWriter out = resp.getWriter();
                    out.print(datafull.toString());
                }

1 个答案:

答案 0 :(得分:0)

您是否尝试过MimeMessage.getContent()?

http://docs.oracle.com/javaee/1.4/api/javax/mail/internet/MimeMessage.html#getContent()