15

好的,所以我使用 .getcontent 并接收 javax.mail.internet.MimeMultipart@fd13b5 等。

我知道我的代码中需要这样的东西,但我不知道到底需要什么。

if (p.isMimeType("text/plain")) {
    pr("This is plain text");
    pr("---------------------------");
    if (!showStructure && !saveAttachments)
    System.out.println((String)p.getContent());
} else if (p.isMimeType("multipart/*")) {
    pr("This is a Multipart");
    pr("---------------------------");
    Multipart mp = (Multipart)p.getContent();
    level++;
    int count = mp.getCount();
    for (int i = 0; i < count; i++)
    dumpPart(mp.getBodyPart(i));
    level--;
} else if (p.isMimeType("message/rfc822")) {
    pr("This is a Nested Message");
    pr("---------------------------");
    level++;
    dumpPart((Part)p.getContent());
    level--;

目前,我正在尝试将所有信息放入字符串中,然后将其显示在 GUI 上,此时我的所有信息都可以正常工作,就像显示为的正文内容一样。javax.mail.internet.MimeMultipart@fd13b5。任何帮助将不胜感激,因为我非常卡住。

package EmailTable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class Email implements ActionListener
{

private mail mail;
private List mails;
private String password;
private String user;
private String getText;
private boolean textIsHtml = false;

public Email(List mails,String password,String user) throws MessagingException,     IOException {

password = "password";
user = "user";
this.mails = mails;
String host = "10..10.10.10";
   Properties properties = System.getProperties(); 
  Session session = Session.getDefaultInstance(properties);
  Store store = session.getStore("pop3");
  store.connect(host, user, password);
  Folder folder = store.getFolder("inbox");
  folder.open(Folder.READ_ONLY);
  Message[] messages = folder.getMessages();

    int length = messages.length-1;
    for (int i = length; i > length-30; i--) {

            mail = new mail();

            mail.setEmail(messages[i].getFrom()[0]);

              String to = InternetAddress.toString(
                                messages[i].getRecipients(Message.RecipientType.TO));
                      if (to != null) {
                          mail.setEmail2(to);
                      }

            mail.setSubject(messages[i].getSubject());

            mail.setDate(messages[i].getSentDate());

            mail.setMessage(messages[i]);

             mail.setContent(((MimeMessage)messages[i]).getContent());



            Email.this.mails.add(mail);
  }

}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
}
4

2 回答 2

8

是的,您必须遍历每个 BodyPart 以了解它的类型,然后相应地获取内容。这是我用来获取消息内容的内容。但是我仍然无法为某些消息获得正确的内容。
实施比尔建议的代码后,编辑
效果更好。

    Object msgContent = messages[i].getContent();

    String content = "";             

     /* Check if content is pure text/html or in parts */                     
     if (msgContent instanceof Multipart) {

         Multipart multipart = (Multipart) msgContent;

         Log.e("BodyPart", "MultiPartCount: "+multipart.getCount());

         for (int j = 0; j < multipart.getCount(); j++) {

          BodyPart bodyPart = multipart.getBodyPart(j);

          String disposition = bodyPart.getDisposition();

          if (disposition != null && (disposition.equalsIgnoreCase("ATTACHMENT"))) { 
              System.out.println("Mail have some attachment");

              DataHandler handler = bodyPart.getDataHandler();
              System.out.println("file name : " + handler.getName());                                 
            }
          else { 
                  content = getText(bodyPart);  // the changed code         
            }
        }
     }
     else                
         content= messages[i].getContent().toString();
于 2012-11-21T08:26:18.023 回答
4

这个解决方案对我来说效果更好。我只是想记录我的电子邮件以用于开发/测试目的。使用 MimeMessage.writeTo(OutputStream) 方法。

void logMimeMessage(MimeMessage msg) throws MessagingException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        msg.writeTo(out);
    } catch (IOException e) {
        logger.catching(new MyException("Cannot log MimeMessage",e));
    }
    logger.error(out.toString());
}

感谢上面@zzzzz 的评论,他链接到这个答案JavaMail - Parsing email content,似乎无法让它工作!(消息.getContent())

于 2015-09-10T22:10:41.323 回答