1

我正在使用 JavaMail API 来获取一些电子邮件。我想获得消息流,然后在另一端将流返回到电子邮件。此外,我不想丢失任何属性,如附件、目的地、发件人、正文等......

怎么能做到这一点?

4

2 回答 2

2

好的...我设法找到了如何实现的答案

我们可以使用

.writeTo(出);

写入OutputStream,向其发送InputStream,最后您可以使用它重建它

Message receivedMail = new MimeMessage( session, inputStream );

问题已解决!

于 2012-10-25T15:52:27.547 回答
1

请检查下面的示例代码:

  URLName url = new URLName("pop3","xxxx",123,"","user","password");
  Session session = Session.getInstance(props, null);
  Store store = new POP3SSLStore(session,url);
  store.connect();

  Folder folder = store.getFolder("INBOX");
  folder.open(Folder.READ_WRITE);

  Message message[] = folder.getMessages(); 

  for (int i=0; i <message.length;i++) {
      Message message = messages[i];

      //Get Message Properties
      System.out.println("From : " + message.getFrom()[0]);
      System.out.println("Subject : " + message.getSubject());
      System.out.println("Sent Date : " + message.getSentDate());

      //Get Input stream for each message
      InputStream is = message.getInputStream();
      .....
      is.close();
  }
  folder.close(false);
  store.close();

向后构造消息:

      MimeMessage newMessage = new MimeMessage(session);
      MimeMultipart mimeMultipart = new MimeMultipart();
      MimeBodyPart attachment = new MimeBodyPart(is);
      attachment.setHeader("Content-Type", "contentType");
      mimeMultipart.addBodyPart(attachment);
      newMessage.setContent(mimeMultipart);
      newMessage.setFrom(InternetAddress.parse("fromAddress")[0]);
      newMessage.setReplyTo(InternetAddress.parse("toAddress"));
      newMessage.setSubject("subject");
于 2012-10-23T14:49:46.633 回答