MimeMessageParser为HtmlEmail抛出ClassCastException

时间:2014-04-20 15:51:32

标签: scala javamail apache-commons-email

我正在尝试使用MimeMessageParser来解析HtmlEmail以获取Html和纯文本正文部分:

def emailForAutomation(email: HtmlEmail): String = {

  email.buildMimeMessage()
  val mimeMessage = email.getMimeMessage()

  val parser = new MimeMessageParser(mimeMessage)
  parser.parse()

  val subject = parser.getSubject()
  val body = parser.getHtmlContent()
  val txt  = parser.getPlainContent()

然而,parser.parse()抛出

java.lang.ClassCastException: javax.mail.internet.MimeMultipart cannot be cast to java.lang.String
    at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:176) ~[commons-email-1.3.2.jar:1.3.2]
    at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:85) ~[commons-email-1.3.2.jar:1.3.2]
    at utils.EmailSender$.emailForAutomation(EmailSender.scala:382) ~[classes/:na]

我在那里做错了什么?该电子邮件是使用apache-common-email API构建的:

val email = new HtmlEmail() // apache-commons-email

email.setHostName(smtpHost)
email.setSmtpPort(smtpPort)

email.setHtmlMsg(html)
email.setTextMsg(plainTextBody)

emailForAutomation(email)  // dump email to file for automation testing

1 个答案:

答案 0 :(得分:0)

它是apache-commons-email 1.3.2中的一个错误。