sendEmail()
Immediately sends an email message
Syntax
SendEmailResult = connection.sendEmail(SingleEmailMessage emails[]);
Sample Code
在Apex中设置件名,发送人,就可以发送邮件。
代码语言:javascript复制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信息
OrgWideEmailAddress
如果设置了组织范围电子邮件地址 Id,则电子邮件标头中使用“组织范围电子邮件地址显示名称”字段,而不是登录用户的“显示名称”。标头中的发送电子邮件地址也设置为组织范围电子邮件地址中定义的字段。
添加成功
代码语言:javascript复制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.');
}
}
}