Sending email in batches through day cq mailer messagegateway failed with error - "The MimeMessage is already built.null" | Community
Skip to main content
New Participant
July 15, 2022
Solved

Sending email in batches through day cq mailer messagegateway failed with error - "The MimeMessage is already built.null"

  • July 15, 2022
  • 2 replies
  • 1402 views

We have a requirement to send the email in batches as we have a huge recipient list and our smtp servers are not allowing that huge list at a time in one go.

So, we are writing a logic to send the email to the recipient list in chunks. While doing that at the line "messageGateway.send(email);", I'm facing an error "The MimeMessage is already built.null".

Could anyone help me on this.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Pulkit_Jain_

@monica_allamneni 

Thanks for sharing the code snippet.

As per [0], when you create an instance of HtmlEmail, set its properties and then invoke the send() method, the object will internally invoke the following methods:

  • buildMimeMessage()
  • sendMimeMessage()

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() base class method, you get the exception found by the original poster.

 

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

 

[0] - Reference: https://stackoverflow.com/questions/16854579/re-sending-multipartemail-with-apache-commons-mail  

2 replies

Pulkit_Jain_
Pulkit_Jain_Accepted solution
Employee
July 18, 2022

@monica_allamneni 

Thanks for sharing the code snippet.

As per [0], when you create an instance of HtmlEmail, set its properties and then invoke the send() method, the object will internally invoke the following methods:

  • buildMimeMessage()
  • sendMimeMessage()

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() base class method, you get the exception found by the original poster.

 

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

 

[0] - Reference: https://stackoverflow.com/questions/16854579/re-sending-multipartemail-with-apache-commons-mail  

New Participant
July 18, 2022

Hi @pulkit_jain_ ,

 

Thanks for acknowledging. But the problem here is I'm not only sending the email but also building the email multiple times as I'm adding the recipient list in a loop.

Also com.day.cq.mailer.MessageGateway do not have the build and send methods separately.

 

Thanks,

Monica

Mayank_Gandhi
Employee
July 19, 2022

@monica_allamneni Yes there is no build method, try this approach and invoke sendEmail now 

 

@Overridepublic List<InternetAddress> sendEmail(final String templatePath, final Map<String, String> emailParams,                    final InternetAddress... recipients) {  List<InternetAddress> failureList = new ArrayList<InternetAddress>();  if (recipients == null || recipients.length <= 0) {    throw new IllegalArgumentException(MSG_INVALID_RECIPIENTS);  }  final MailTemplate mailTemplate = this.getMailTemplate(templatePath);  final Class<? extends Email> mailType = this.getMailType(templatePath);  final MessageGateway<Email> messageGateway = messageGatewayService.getGateway(mailType);  for (final InternetAddress address : recipients) {    try {      // Get a new email per recipient to avoid duplicate attachments
      final Email email = getEmail(mailTemplate, mailType, emailParams);      email.setTo(Collections.singleton(address));      messageGateway.send(email);    } catch (Exception e) {      failureList.add(address);      log.error("Error sending email to [ " + address + " ]", e);    }  }  re

 

Mayank_Gandhi
Employee
July 15, 2022

@monica_allamneni little more context on implementation pls.

New Participant
July 18, 2022

Hi @mayank_gandhi ,

 

Below is the logic I'm using. When I have written the messageGateway.send(email) in a loop, It is throwing the error "The MimeMessage is already built.null". But as per our requirement we have to send the emails with recipient list restricted to 90 at one time. So we have to use the loop.