Email Via Apex using Email Templates :
Contact con = [Select id from contact Limit 1];
List<String> emailsAll = new List<String>();
emailsAll.add('toEmailAddress@gmail.com');
List<String> emailsCC = new List<String>();
emailsCC.add('cCEmailAddress@gmail.com');
String TemplateId = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Multi_Language_Template_Test' LIMIT 1].Id;
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
// Step 1: Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
mail.setToAddresses(emailsAll);
mail.setCcAddresses(emailsCC);
// Step 3: Set who the email is sent from
mail.setReplyTo('noreply@gmail.com'); // change it with your/Company mail address.
mail.setSenderDisplayName('Salesforce User');
//Step 4 : when Using Template Id
mail1.setTargetObjectId(con.Id); //Mandatory while using Template(ID of contact, lead, or user)
mail1.setTemplateId(TemplateId);
/*OR*/
// Step 4. Set email contents - you can use variables! when not using Template
mail.setSubject(mSubject);
mail.setHtmlBody(mbody);
// Step 5. Attach file! (Not Mendatory)
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId =: parentId ORDER BY CreatedDate DESC Limit : 1])
{
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}
mail.setFileAttachments(fileAttachments);
// Step 6. Add your email to the master list
mails.add(mail);
// Step 7: Send all emails in the master list
Messaging.sendEmail(mails);
Contact con = [Select id from contact Limit 1];
List<String> emailsAll = new List<String>();
emailsAll.add('toEmailAddress@gmail.com');
List<String> emailsCC = new List<String>();
emailsCC.add('cCEmailAddress@gmail.com');
String TemplateId = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Multi_Language_Template_Test' LIMIT 1].Id;
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
// Step 1: Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
mail.setToAddresses(emailsAll);
mail.setCcAddresses(emailsCC);
// Step 3: Set who the email is sent from
mail.setReplyTo('noreply@gmail.com'); // change it with your/Company mail address.
mail.setSenderDisplayName('Salesforce User');
//Step 4 : when Using Template Id
mail1.setTargetObjectId(con.Id); //Mandatory while using Template(ID of contact, lead, or user)
mail1.setTemplateId(TemplateId);
/*OR*/
// Step 4. Set email contents - you can use variables! when not using Template
mail.setSubject(mSubject);
mail.setHtmlBody(mbody);
// Step 5. Attach file! (Not Mendatory)
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId =: parentId ORDER BY CreatedDate DESC Limit : 1])
{
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}
mail.setFileAttachments(fileAttachments);
// Step 6. Add your email to the master list
mails.add(mail);
// Step 7: Send all emails in the master list
Messaging.sendEmail(mails);
Comments
Post a Comment