Code Example sendmail (salesforce apex code)
public static void sendMailSupport(String subject, String message,String receiver){
sendMailSupport(subject,message,receiver,null);
}
public static void sendMailSupport(String subject, String message,String receiver, Messaging.EmailFileAttachment csvAttc){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if(String.isBlank(receiver)){
mail.setTargetObjectId(UserInfo.getUserId());
mail.setTreatTargetObjectAsRecipient(false);
mail.setToAddresses(new String[] {Settings__c.getOrgDefaults().EmailSupportWebService__c});
} else {
mail.setTargetObjectId(receiver);
}
mail.setSenderDisplayName('Apex Debugger');
mail.setSubject(subject);
mail.setSaveAsActivity(false);
mail.setPlainTextBody(message);
if(csvAttc != null){
mail.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
}
mail.setReplyTo('noreply@domain.it');
if(Test.isRunningTest()==false)Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
public static void sendMail(String address, EmailTemplate emailTemplate) {
String emailAddressSupportDS = Settings__c.getInstance().Email_Support_c;
OrgWideEmailAddress[] owea = [SELECT Id FROM OrgWideEmailAddress WHERE Address = :emailAddressSupportDS];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if (owea.size() > 0 ) {
mail.setOrgWideEmailAddressId(owea.get(0).Id);
}
String[] toAddresses = new String[] {address};
mail.setTemplateID(emailTemplate.Id);
mail.toAddresses = toAddresses;
mail.setPlainTextBody(emailTemplate.Body);
Messaging.SendEmailResult[] results = Messaging.sendEmail(
new Messaging.SingleEmailMessage[] { mail });
}