高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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);
    }
}