meta data for this page
  •  

Log full stacktrace of an email exception

The de.hybris.platform.acceleratorservices.email.impl.DefaultEmailService logs with debug level any email exception but in production it is a bad issue to turn on the debug level.

The following groovy script can be ran on HAC > Console > Scripting languages and will show the full stack trace of the email exception. Please remember to enter the PK of the email message which you want to send.

import de.hybris.platform.acceleratorservices.email.strategy.EmailAddressFetchStrategy
import de.hybris.platform.acceleratorservices.model.email.EmailAddressModel
import de.hybris.platform.acceleratorservices.model.email.EmailAttachmentModel
import de.hybris.platform.acceleratorservices.model.email.EmailMessageModel
import de.hybris.platform.catalog.CatalogService
import de.hybris.platform.catalog.CatalogVersionService
import de.hybris.platform.core.model.media.MediaModel
import de.hybris.platform.servicelayer.config.ConfigurationService
import de.hybris.platform.servicelayer.media.MediaService
import de.hybris.platform.servicelayer.model.ModelService
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery
import de.hybris.platform.servicelayer.search.FlexibleSearchService
import de.hybris.platform.util.mail.MailUtils
import org.apache.commons.collections.CollectionUtils
import org.apache.commons.lang.StringUtils
import org.apache.commons.mail.EmailException
import org.apache.commons.mail.HtmlEmail

import javax.activation.DataSource
import javax.mail.internet.AddressException
import javax.mail.internet.InternetAddress
import javax.mail.util.ByteArrayDataSource
import java.nio.channels.ScatteringByteChannel
import java.nio.charset.StandardCharsets

void setAddresses(final EmailMessageModel message, final HtmlEmail email, final List<EmailAddressModel> toAddresses)
        throws EmailException {
    if (CollectionUtils.isNotEmpty(toAddresses)) {
        email.setTo(getAddresses(toAddresses));
    } else {
        throw new IllegalArgumentException("message has no To addresses");
    }

    final List<EmailAddressModel> ccAddresses = message.getCcAddresses();
    if (ccAddresses != null && !ccAddresses.isEmpty()) {
        email.setCc(getAddresses(ccAddresses));
    }

    final List<EmailAddressModel> bccAddresses = message.getBccAddresses();
    if (bccAddresses != null && !bccAddresses.isEmpty()) {
        email.setBcc(getAddresses(bccAddresses));
    }
}

void addReplyTo(final EmailMessageModel message, final HtmlEmail email) throws EmailException {
    // Add the reply to if specified
    final String replyToAddress = message.getReplyToAddress();
    if (replyToAddress != null && !replyToAddress.isEmpty()) {
        email.setReplyTo(Collections.singletonList(createInternetAddress(replyToAddress, null)));
    }
}

void processAttachmentsSuccessful(final HtmlEmail email, final List<EmailAttachmentModel> attachments, MediaService mediaService) {
    if (attachments != null && !attachments.isEmpty()) {
        for (final EmailAttachmentModel attachment : attachments) {
            final DataSource dataSource = new ByteArrayDataSource(mediaService.getDataFromMedia(attachment),
                    attachment.getMime());
            email.attach(dataSource, attachment.getRealFileName(), attachment.getAltText());
        }
    }
}

String getBody(final EmailMessageModel message, MediaService mediaService) {
    if (message.getBodyMedia() != null) {
        final MediaModel media = message.getBodyMedia();
        return new String(mediaService.getDataFromMedia(media), StandardCharsets.UTF_8);
    } else {
        return message.getBody();
    }
}

String nullifyEmpty(final String str) {
    if (str != null && str.isEmpty()) {
        return null;
    }
    return str;
}

Collection<InternetAddress> getAddresses(final List<EmailAddressModel> emailAddresses) {
    final Collection<InternetAddress> internetAddresses = new ArrayList<InternetAddress>();

    for (final EmailAddressModel emailAddress : emailAddresses) {
        internetAddresses.add(createInternetAddress(emailAddress.getEmailAddress(), emailAddress.getDisplayName()));
    }
    return internetAddresses;
}

InternetAddress createInternetAddress(final String emailAddress, final String displayName) throws EmailException {
    try {
        final InternetAddress address = new InternetAddress(emailAddress);
        address.setPersonal(StringUtils.isNotBlank(displayName) ? displayName : emailAddress);
        address.validate();
        return address;
    }
    catch (final AddressException e) {
        throw new EmailException(e);
    }
    catch (final UnsupportedEncodingException e) {
        throw new EmailException(e);
    }
}

List<String> convertToStrings(final List<EmailAddressModel> addresses) {
    final List<String> strings = new ArrayList<String>();
    if (addresses != null && !addresses.isEmpty()) {
        for (final EmailAddressModel item : addresses) {
            strings.add(item.getEmailAddress());
        }
    }
    return strings;
}

try {

// Load email message by PK
    FlexibleSearchService localFlexibleSearchService = flexibleSearchService
    EmailMessageModel message = localFlexibleSearchService.searchUnique(new FlexibleSearchQuery("Select {pk} from {EmailMessage} where {pk} = 8825650223168"))

    final HtmlEmail email = (HtmlEmail) MailUtils.getPreConfiguredEmail();
    email.setCharset("UTF-8");

    final List<EmailAddressModel> toAddresses = message.getToAddresses();
    setAddresses(message, email, toAddresses);

    final EmailAddressModel fromAddress = message.getFromAddress();
    email.setFrom(fromAddress.getEmailAddress(), nullifyEmpty(fromAddress.getDisplayName()));

    addReplyTo(message, email);

    email.setSubject(message.getSubject());
    email.setHtmlMsg(getBody(message, mediaService));

// To support plain text parts use email.setTextMsg()

    final List<EmailAttachmentModel> attachments = message.getAttachments();
    processAttachmentsSuccessful(email, attachments, mediaService)

// Important to log all emails sent out
    println("Sending Email [" + message.getPk() + "] To [" + convertToStrings(toAddresses) + "] From ["
            + fromAddress.getEmailAddress() + "] Subject [" + email.getSubject() + "]");

// Send the email and capture the message ID
    final String messageID = email.send();

    message.setSent(true);
    message.setSentMessageID(messageID);
    message.setSentDate(new Date());
    modelService.save(message);

} catch (Exception e) {
    println("The email couldn't be sent")
    e.printStackTrace(System.err)
}
println "OK"

–Based on SAP Commerce 2211

Discussion

Enter your comment. Wiki syntax is allowed: