public without sharing class BatchEmailUtil {
    /** メッセージリスト */
    private List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
    /**
     * メッセージ追加
     * @param message メッセージ
     */
    private void addMessage(Messaging.SingleEmailMessage message) {
        if(messages == null) {
            messages = new List<Messaging.SingleEmailMessage>();
        }
        messages.add(message);
    }
    /**
     * メール送信
     */
    public void send() {
        if (System.Test.isRunningTest() == false
                && messages != null && messages.size() > 0) {
            Messaging.sendEmail(messages);
        }
    }
    public void successMail(String title, Integer successCount) {
        successMail(null, null, title, successCount, '');
    }
    public void successMail(String[] toStr, String[] ccStr, String title, Integer successCount) {
        successMail(toStr, ccStr, title, successCount, '');
    }
    public void successMail(String[] toStr, String[] ccStr, String title, Integer successCount, String text) {
        String subject = title+'执行成功 结束时间:' + Datetime.now().format();
        text += '\n*******处理结果*******\n'
                + '总件数 : ' + String.valueOf(successCount)+'\n'
                + '*********结束*********\n';
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setReplyTo(UserInfo.getUserEmail());
        message.setCharset('UTF-8');
        // 差出人は通知用メールアドレス
        //message.setSenderDisplayName(System.Label.mms_info_mailaddress);
        //OrgWideEmailAddress owa = CommonUtil.getOrgWideEmailAddress(System.Label.mms_info_mailaddress);
        //message.setOrgWideEmailAddressId(owa.Id);
        // 送信人
        if (toStr == null || toStr.size() == 0) {
            message.setToAddresses(new String[] {UserInfo.getUserEmail()});
        } else {
            message.setToAddresses(toStr);
        }
        // CC
        if (ccStr != null && ccStr.size() != 0) {
            message.setCcAddresses(ccStr);
        }
        // 件名
        message.setSubject(subject);
        // 本文
        message.setPlainTextBody(text);
        message.setUseSignature(false);
        addMessage(message);
    }
    //20210830 you 邮件 CSV存档邮件推送
    public void successMail(String[] toStr, String[] ccStr, String title, String errMsg,Integer successCount, Integer failedCount, String text) {
        String subject = title+'执行成功 结束时间:' + Datetime.now().format();
        text += '\n*******处理结果*******\n'
                + '总件数： ' + String.valueOf(successCount)+'\n'
                + '成功存档件数 : ' + String.valueOf(successCount-failedCount)+'\n'
                + '无数据未存档件数 : ' + String.valueOf(failedCount)+'\n'
                + '*********未存档信息*********\n'
                + errMsg + '\n'
                + '*********结束*********\n';
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setReplyTo(UserInfo.getUserEmail());
        message.setCharset('UTF-8');
        // 差出人は通知用メールアドレス
        //message.setSenderDisplayName(System.Label.mms_info_mailaddress);
        //OrgWideEmailAddress owa = CommonUtil.getOrgWideEmailAddress(System.Label.mms_info_mailaddress);
        //message.setOrgWideEmailAddressId(owa.Id);
        // 送信人
        if (toStr == null || toStr.size() == 0) {
            message.setToAddresses(new String[] {UserInfo.getUserEmail()});
        } else {
            message.setToAddresses(toStr);
        }
        // CC
        if (ccStr != null && ccStr.size() != 0) {
            message.setCcAddresses(ccStr);
        }
        // 件名
        message.setSubject(subject);
        // 本文
        message.setPlainTextBody(text);
        message.setUseSignature(false);
        addMessage(message);
    }
    public void failedMail(String title, String errMsg
            , Integer totalCount, Integer doneCount, Integer failedCount) {
        failedMail(null, null, title, errMsg, totalCount, doneCount, failedCount, '');
    }
    public void failedMail(String[] toStr, String[] ccStr, String title, String errMsg
            , Integer totalCount, Integer doneCount, Integer failedCount) {
        failedMail(toStr, ccStr, title, errMsg, totalCount, doneCount, failedCount, '');
    }
    public void failedMail(String[] toStr, String[] ccStr, String title, String errMsg
            , Integer totalCount, Integer doneCount, Integer failedCount, STring text) {
        String subject = title+'执行失败 结束时间:' + Datetime.now().format();
        text +=
                  '*******处理结果*******\n\n'
                + '总件数 : ' + totalCount+'\n'
                + '失败件数 : ' + failedCount +'\n'
                + '处理完毕件数 : ' + doneCount+'\n'+
                + '*******错误信息*******\n' +
                + errMsg + '\n'
                + '*********结束*********\n';
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setReplyTo(UserInfo.getUserEmail());
        message.setCharset('UTF-8');
        // 送信人
        if (toStr == null || toStr.size() == 0) {
            message.setToAddresses(new String[] {UserInfo.getUserEmail()});
        } else {
            message.setToAddresses(toStr);
        }
        // CC
        Set<String> ccEmailSet = new Set<String>();
        for (ApexEmailNotification tmpEmailObj : [SELECT Email, UserId, User.Email
            FROM ApexEmailNotification]) {
                if (String.isBlank(tmpEmailObj.UserId)) {
                    for (String emailAddress : tmpEmailObj.Email.split(',')) {
                        ccEmailSet.add(emailAddress.trim());
                    }
                } else {
                    ccEmailSet.add(tmpEmailObj.user.Email);
                }
        }
        if (ccStr != null && ccStr.size() > 0) {
            for (String emailAddress : ccStr) {
                ccEmailSet.add(emailAddress);
            }
        }
        message.setCcAddresses(new List<String>(ccEmailSet));
        // 件名
        message.setSubject(subject);
        // 本文
        message.setPlainTextBody(text);
        message.setUseSignature(false);
        addMessage(message);
    }
    //20210830 you 邮件 CSV存档邮件推送
    public void failedMail(String[] toStr, String[] ccStr, String title, String errMsg
            , Integer totalCount, Integer doneCount,Integer nullCount, Integer failedCount, STring text) {
        String subject = title+'执行失败 结束时间:' + Datetime.now().format();
        text +=
                  '*******处理结果*******\n\n'
                + '总件数 : ' + totalCount+'\n'
                + '成功存档件数 : ' + doneCount+'\n'
                + '无数据未存档件数 : ' + nullCount+'\n'
                + '失败件数 : ' + failedCount +'\n'+
                + '*******错误信息*******\n' +
                + errMsg + '\n'
                + '*********结束*********\n';
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setReplyTo(UserInfo.getUserEmail());
        message.setCharset('UTF-8');
        // 送信人
        if (toStr == null || toStr.size() == 0) {
            message.setToAddresses(new String[] {UserInfo.getUserEmail()});
        } else {
            message.setToAddresses(toStr);
        }
        // CC
        Set<String> ccEmailSet = new Set<String>();
        for (ApexEmailNotification tmpEmailObj : [SELECT Email, UserId, User.Email
            FROM ApexEmailNotification]) {
                if (String.isBlank(tmpEmailObj.UserId)) {
                    for (String emailAddress : tmpEmailObj.Email.split(',')) {
                        ccEmailSet.add(emailAddress.trim());
                    }
                } else {
                    ccEmailSet.add(tmpEmailObj.user.Email);
                }
        }
        if (ccStr != null && ccStr.size() > 0) {
            for (String emailAddress : ccStr) {
                ccEmailSet.add(emailAddress);
            }
        }
        message.setCcAddresses(new List<String>(ccEmailSet));
        // 件名
        message.setSubject(subject);
        // 本文
        message.setPlainTextBody(text);
        message.setUseSignature(false);
        addMessage(message);
    }
}