sendEmail()
Immediately sends an email message
SendEmailResult = connection.sendEmail(SingleEmailMessage emails[]);
在Apex中设置件名,发送人,就可以发送邮件。
public with sharing class SendEmailUtils {
public static void sendMailFromEx() {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] { '12345@qq.com' });
email.setSubject('my subject');
email.setPlainTextBody('plain text body');
email.setSenderDisplayName('Salesforce Support');
List<Messaging.SendEmailResult> results =
Messaging.sendEmail(new Messaging.Email[] { email });
if (!results.get(0).isSuccess()) {
System.StatusCode statusCode = results.get(0).getErrors()[0].getStatusCode();
String errorMessage = results.get(0).getErrors()[0].getMessage();
system.debug(errorMessage);
} else {
system.debug('The email was sent successfully.');
}
}
}
在开发者Console中调用上边的Apex类,发送邮件。
Log信息
如果设置了组织范围电子邮件地址 Id,则电子邮件标头中使用“组织范围电子邮件地址显示名称”字段,而不是登录用户的“显示名称”。标头中的发送电子邮件地址也设置为组织范围电子邮件地址中定义的字段。
添加成功
public with sharing class SendEmailUtils {
public static void sendMailFromEx() {
List<OrgWideEmailAddress> owea = [select id from OrgWideEmailAddress limit 1];
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] { '123456@163.com' });
email.setSubject('my subject');
email.setPlainTextBody('plain text body');
If (owea != null && owea.size() > 0) {
email.setOrgWideEmailAddressId(owea.get(0).id);
} else {
email.setSenderDisplayName('Salesforce Support');
}
List<Messaging.SendEmailResult> results =
Messaging.sendEmail(new Messaging.Email[] { email });
if (!results.get(0).isSuccess()) {
System.StatusCode statusCode = results.get(0).getErrors()[0].getStatusCode();
String errorMessage = results.get(0).getErrors()[0].getMessage();
system.debug(errorMessage);
} else {
system.debug('The email was sent successfully.');
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。