简体   繁体   中英

How to read attachment from mail?

I'm using JavaMail to reading email messages. If I get normal messages with attachment files it's all file I can read it but if I receive forwarded message I couldn't get the attachment. This is my code:

class Msg(src: IMAPMessage) {
    lazy val multipart: MimeMultipart = src.getContent.asInstanceOf[MimeMultipart]
    def parts = (0 until multipart.getCount)
      .map(multipart.getBodyPart)

    def files = {
      parts
          .filter(s => {
            println(s + " " + s.getFileName + " " + s.getContent + " " + s.getLineCount)
            true})
        .filter(_.getFileName != null)
        .map(part => part.getFileName -> part.getContent.toString)
    }
  }

object MailMain {
  def main(args: Array[String]): Unit = {
    new ImapMail("host_name", "user_name", "password", ssl)
      .messages(true)
      .foreach(m => println(m.files))
  }

And when I try to get content via part.getContent.toString I receive that com.sun.mail.util.BASE64DecoderStream@67d48005 . What is my problem?

Obviously that content is a Stream, if you want to see its content.

// Assume a obj is an instance of com.sun.mail.util.BASE64DecoderStream.
if (obj.isInstanceOf[InputStream]) {
      val is: InputStream = obj.asInstanceOf[InputStream]
      var ch: Int = -1
      while ((ch = is.read()) != -1) System.out.write(ch)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM