我正在尝试使用 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