class MailerWithTemplate {

    public MailerResult sendMailAsSeparateMails(List<? extends OLATPrincipal> recipientsTO, List<? extends OLATPrincipal> recipientsCC,
            List<? extends OLATPrincipal> recipientsBCC, MailTemplate template, OLATPrincipal sender) {
        MailerResult result = new MailerResult();
        if (webappAndMailHelper.getMailhost() == null) {
            result.setReturnCode(MailerResult.MAILHOST_UNDEFINED);
            log.warn("sendMailAsSeparateMails - webappAndMailHelper.getMailhost(): " + webappAndMailHelper.getMailhost());
            return result;
        }
        boolean isMailSendToRecipient = false;
        if (recipientsTO != null) {
            for (OLATPrincipal recipient : recipientsTO) {
                // populate velocity context with variables
                VelocityContext context = new VelocityContext();
                template.putVariablesInMailContext(context, recipient);
                sendWithContext(context, recipient, null, null, template, sender, result);
                if (isMailSentToRecipient(result, recipient)) {
                    isMailSendToRecipient = true;
                }
            }
        }
        if (recipientsCC != null) {
            for (OLATPrincipal recipient : recipientsCC) {
                List<OLATPrincipal> cc = new ArrayList<OLATPrincipal>();
                cc.add(recipient);
                // populate velocity context with variables
                VelocityContext context = new VelocityContext();
                template.putVariablesInMailContext(context, recipient);
                sendWithContext(context, null, cc, null, template, sender, result);
                if (isMailSentToRecipient(result, recipient)) {
                    isMailSendToRecipient = true;
                }
            }
        }
        if (recipientsBCC != null) {
            for (OLATPrincipal recipient : recipientsBCC) {
                // populate velocity context with variables
                VelocityContext context = new VelocityContext();
                template.putVariablesInMailContext(context, recipient);
                sendWithContext(context, recipient, null, null, template, sender, result);
                if (isMailSentToRecipient(result, recipient)) {
                    isMailSendToRecipient = true;
                }
            }
        }
        if (!isMailSendToRecipient) {
            if (result.getReturnCode() == MailerResult.OK) {
                result.setReturnCode(MailerResult.RECIPIENT_ADDRESS_ERROR);
            }
            log.warn("sendMailAsSeparateMails - result.getReturnCode(): " + result.getReturnCode() + " . See MailerResult for return codes.");
        }
        return result;
    }

    private  MailerWithTemplate();
    private  MailerWithTemplate(MailPackageStaticDependenciesWrapper webappAndMailHelper);

}