使用 apache commons 邮件重新发送 MultiPartEmail [英] Re-sending MultiPartEmail with apache commons mail

查看:29
本文介绍了使用 apache commons 邮件重新发送 MultiPartEmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的场景:

  1. 一些电子邮件发送 bean 被调用,其中包含一些构建邮件所需的参数.
  2. 它创建 MultiPartEmail 并将其排队等待发送,这是由后台的单独线程完成的,调用者不关心它是否已发送.
  3. 发送线程接收排队的电子邮件实例并执行 email.send() - 就这样了.
  4. 当投递失败时,bean 会每 5 分钟尝试重新发送一次邮件 3 次,然后放弃.

问题:

我不知道如何处理 #4.我手头的东西是以前构造的 MultiPartEmail 实例,但它无法发送.显然再次执行 email.send() 会抛出这个:

I can't figure out how to handle #4. What I've got at hand is previously constructed MultiPartEmail instance which failed to be sent. Apparently doing email.send() again throws this:

java.lang.IllegalStateException:MimeMessage 已经构建.

有没有办法重置这个非法状态,以便可以重新使用消息.我真的没有从头开始创建新实例的方法 - 调用者早已不复存在,它会使整个机制变得非常丑陋,而无法使用已经构建的对象.我想我在这里遗漏了一些非常简单的东西..

Is there a way to reset this illegal state so that message can be re-used. I don't really have means of creating new instance from scratch - the caller is long gone, and it will make the whole mechanism quite ugly without being able to use already built object. I think I'm missing something very simple here..

推荐答案

我看到这是一个老问题,但我遇到了同样的问题并找到了解决方案.

I see this is an old question, but I just hit the same problem and I found the solution.

当您创建 MultiPartEmail(或 HtmlEmail)的实例,设置其属性,然后调用 send() 方法时,对象将在内部调用以下方法:

When you create an instance of MultiPartEmail (or HtmlEmail), set its properties, and then invoke the send() method, the object will internally invoke the following methods:

  • buildMimeMessage()
  • sendMimeMessage()

多次调用 sendMimeMessage() 是可以的,例如发送并重试的场景.问题是 buildMimeMessage() 只能被调用一次.当您依赖于基本 Email 类的 send() 方法时,您会得到原始发布者发现的异常.

It is ok to invoke sendMimeMessage() multiple times, such as a send-with-retry scenario. The problem is that buildMimeMessage() can only be invoked once. When you rely on the send() method of the base Email class, you get the exception found by the original poster.

当您的 Email 对象是 MultiPartEmail 时,解决方案是使用我刚刚提到的两种方法.您显式调用 buildMimeMessage() 一次,然后调用 sendMimeMessage() 一次或多次.

The solution is to use the two methods I just mentioned when your Email object is a MultiPartEmail. You explicitly invoke buildMimeMessage() once, then invoke sendMimeMessage() one or more times.

这篇关于使用 apache commons 邮件重新发送 MultiPartEmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆