使用Apache Commons Mail重新发送MultiPartEmail [英] Re-sending MultiPartEmail with apache commons mail

查看:485
本文介绍了使用Apache Commons Mail重新发送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()只能被调用一次.当您依赖基类Emailsend()方法时,您会得到原始张贴者发现的异常.

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 Mail重新发送MultiPartEmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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