Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Avatar

Level 1

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.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

@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  

View solution in original post

2 Replies

Avatar

Level 1

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.

 

Monica_Allamneni_1-1658124234926.png

 

 

Avatar

Correct answer by
Employee Advisor

@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  

Avatar

Level 1

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

Avatar

Employee Advisor

@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