引用之前的通信 - 正确格式化

时间:2018-01-24 17:01:15

标签: java spring spring-integration javamail

我正在编写一个服务(Spring Integration + JavaMail API),它会自动回复收到的消息,并且我在包含上一个消息正文时遇到了问题。

我该如何正确处理?我想保留相同的格式,即Gmail / Thunderbird使用的格式。我包含了显示我是如何完成它的代码片段,但是我可以使用其他一些解决方案吗?也许有些图书馆?常用表达?我对stackoverflow问题感到很满意,但回答告诉我必须自己处理它并不是很有用。

我愿意接受建议。提前谢谢!

在Gmail上,它看起来像这样: enter image description here

雷鸟:

enter image description here

fun prepareReplyFor(mimeMessage: MimeMessage): MimeMessage
{
    val response = mimeMessage.reply(SHOULD_REPLY_TO_ALL) as MimeMessage
    val mailFormatter = MailFormatter()

    val botText = MimeBodyPart()
    botText.setContent("This message was generated.\n", "text/plain")

    val previousCorrespondence = MimeBodyPart()
    previousCorrespondence.setContent(mailFormatter.formatPrevious(mimeMessage), "text/plain")

    val responseBody = MimeMultipart()
    responseBody.addBodyPart(botText)
    responseBody.addBodyPart(previousCorrespondence)

    response.setContent(responseBody)

    return response
}

MailFormatter:

const val CORRESPONDENCE_FORMAT_REGEX = "(?m)^"
const val FIRST_BODY_PART = 0

class MailFormatter
{
  fun formatPrevious(mimeMessage: MimeMessage): String
  {
    val previousMessage = mimeMessage.content
    var formattedQuote = "In reply to:\n\n"

    if (previousMessage is String)
    {
        formattedQuote += previousMessage.replace(CORRESPONDENCE_FORMAT_REGEX.toRegex(), "> ")
    }
    else if (previousMessage is MimeMultipart)
    {
        formattedQuote += (previousMessage.getBodyPart(FIRST_BODY_PART).content as String).replace(CORRESPONDENCE_FORMAT_REGEX.toRegex(), "> ")
    }

    return formattedQuote
  }
}

0 个答案:

没有答案