How to support multiple toAddress CC BCC and attachments in hybris email?


We want to send email to multiple addresses, also want to support the email attachments. Unfortunately, it's not there in OOTB.


We have to define our custom attributes in BusinessProcess itemType to stores the values and access it in the later process. Also, we have to override generate method of DefaultEmailGenerationService, as it is responsible to generate EmailMessageModel. We will set attachments and other fields to generated emailMessageModel only. Let's see it step by step.

1) Define custom attribute in BusinessProcessModel
To do so, you have to define attributes like below in your *-items.xml file. Now build your server(ant clean all), which will generate BusinessProcessModel with our custom attributes, which can be accessed using setter and getter methods.
Since we already have OOTB EmailAttachment itemType, we can use it for emailAttachments attribute. we just need to define 1..n relation between BusinessProcess and EmailAttachment. For toAddresses, ccAddresses and bccAddresses we can use String collection.
<!-- 1..n relation between BusinessProcess and EmailAttachment -->
    <relation code="BusinessProcess2AttachmentsRel" autocreate="true"
        generate="true" localized="false">
        <description>This relation links email message with specific business
        <sourceElement qualifier="process" type="BusinessProcess"
            cardinality="one" />
        <targetElement qualifier="emailAttachments" type="EmailAttachment"
            cardinality="many" collectiontype="list" />

<itemtype code="BusinessProcess" autocreate="false" generate="false">
        <attribute qualifier="toAddresses" type="StringCollection">
            <persistence type="property" />
            <description>Attribute contains toAddresses that is used in this
        <attribute qualifier="ccAddresses" type="StringCollection">
            <persistence type="property" />
            <description>Attribute contains ccAddresses that is used in this
        <attribute qualifier="es" type="StringCollection">
            <persistence type="property" />
            <description>Attribute contains bccAddresses that is used in this

2) Override DefaultEmailGenerationService
OOTB EmailMessageModel already have all attributes which we want to set(toAddresses, ccAddresses etc.), Just that it always sets empty. Instead of emplty list, we will save actual value by overriding generate method of DefaultEmailGenerationService.
public class ExtendedDefaultEmailGenerationService extends DefaultEmailGenerationService



    public EmailMessageModel generate(BusinessProcessModel businessProcessModel, final EmailPageModel emailPageModel)
        //Let's first call OOTB generate method to generate emailMessageModel
        EmailMessageModel emailMessageModel = super.generate(businessProcessModel, emailPageModel);

        // Now, we retrieve values of our fileds from businessProcessModel and set them to the emailMessageModel
        List<EmailAddressModel> toAddresses = createEmailAddressForEmail(businessProcessModel.getToAddresses());
        List<EmailAddressModel> ccAddresses = createEmailAddressForEmail(businessProcessModel.getCcAddresses());
        List<EmailAddressModel> bccAddresses = createEmailAddressForEmail(businessProcessModel.getBccAddresses());
        List<EmailAttachmentModel> emailAttachments = businessProcessModel.getEmailAttachments();

        if (CollectionUtils.isNotEmpty(toAddresses))

        if (CollectionUtils.isNotEmpty(ccAddresses))

        if (CollectionUtils.isNotEmpty(bccAddresses))

        if (CollectionUtils.isNotEmpty(emailAttachments))

    private List<EmailAddressModel> createEmailAddressForEmail(List<String> emails)
        final List<EmailAddressModel> emailAddressModelList = new ArrayList<EmailAddressModel>();
        if (CollectionUtils.isNotEmpty(emails))
            for (String email : emails)
                final EmailAddressModel emailAddressModel = getEmailService().getOrCreateEmailAddressForEmail(email,
        return null;

3) Overridde emailGenerationService alias with our class bean
<alias alias="emailGenerationService" name="extendedDefaultEmailGenerationService"/>
<bean id="extendedDefaultEmailGenerationService" class=""

4) Cerate businessProcessModel and set the values
Create businessProcessModel instance, set all attributes and stat the process to send the email with attachemts and other fileds.
public void processEmail(List<String> toAddresses, List<String> ccAddresses, List<String> bccAddresses, List<EmailAttachmentModel> emails)

        BusinessProcessModel businessProcessModel = getBusinessProcessService().createProcess("myProcess-"+ System.currentTimeMillis(), "processDefination");

        //... set site, lang, currency etc.



How to change fromEmail Address of an email in Hybris?

Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 6+ years experience in Hybris, Java, J2EE. Extensive hands-on experience in SAP Hybris development, third-party integrations with Hybris, project architecture and design... Read more


  1. Will this change the TO email addresses of every mail that is flowing out.?


Post a Comment

Popular posts from this blog

Hybris flexible search query examples

How to remove or update all data records in Hybris?

Hybris backoffice customization