public without sharing class PretechBatchEmailUtil {
|
/** メッセージリスト */
|
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'
|
+ '成功件数 : ' + 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);
|
}
|
public void failedMail(String title, String errMsg
|
, Integer totalCount, Integer successCount, Integer failedCount) {
|
failedMail(null, null, title, errMsg, totalCount, successCount, failedCount, '',true);
|
}
|
public void failedMail(String[] toStr, String[] ccStr, String title, String errMsg
|
, Integer totalCount, Integer successCount, Integer failedCount) {
|
failedMail(toStr, ccStr, title, errMsg, totalCount, successCount, failedCount, '',true);
|
}
|
public void failedMail(String[] toStr, String[] ccStr, String title, String errMsg
|
, Integer totalCount, Integer successCount, Integer failedCount,
|
STring text, boolean sendToManager) {
|
String subject = title + '执行失败 结束时间:' + Datetime.now().format();
|
text +=
|
'*******处理结果*******\n\n'
|
+ '总件数 : ' + totalCount + '\n'
|
+ '失败件数 : ' + failedCount + '\n'
|
+ '成功件数 : ' + successCount + '\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>();
|
string query = 'select id , Email from user where id in (';
|
query += System.Label.batchEmailAlert.replaceall('\'','\\\'') +')';
|
List<user> ccUserList = Database.Query(query);
|
|
if(sendToManager){
|
query = 'select id , Email from user where id in (';
|
query += System.Label.batchEmailAlertCC.replaceall('\'','\\\'') +')';
|
List<user> tempUserList = Database.Query(query);
|
ccUserList.addall(tempUserList);
|
}
|
for (user ccUser : ccUserList) {
|
if (!String.isBlank(ccUser.Email)) {
|
ccEmailSet.add(ccUser.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);
|
}
|
}
|