Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

How to Send the Email with Attachment Using Apex Class

Be First!
by March 1, 2013 APEX, SALESFORCE

How to Send the Email with Attachment Using Apex Class?

Using below code we can set the To, CC, BCc, Body Content with Email Signature and with the Attachment from related SFDC Attachment Object,


public void sendEmailWithAttachment() {

set<string> setEmailAddresses = new set<string>();

Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType('application/pdf');
attach.setFileName('testPdf.pdf');
attach.setInline(false);
attach.Body = 'BODY CONTENT HERE';
string toMail = 'info@theblogreaders.com'
subject = 'SEND EMAIL WITH ATTACHMENT USING APEX CLASS';

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(true);
setEmailAddresses.clear();
//Set To Addresses
if(toMail != null && toMail != '') {
setEmailAddresses.add(toMail);
}
if(setEmailAddresses.size() > 0){
mail.setToAddresses(convertToList(setEmailAddresses));
}
//Set CC Addresses
setEmailAddresses.clear();
if(ccid != null && ccid != ''){
setEmailAddresses.addAll(ccid.split('[,;]'));
}
if(setEmailAddresses.size() > 0){
mail.setCcAddresses(convertToList(setEmailAddresses));
}
//Set BCC Address.
setEmailAddresses.clear();
if(bccid != null && bccid != '') {
setEmailAddresses.addAll(bccid.split('[,;]'));
}
if(setEmailAddresses.size() > 0){
mail.setBccAddresses(convertToList(setEmailAddresses));
}

mail.setSubject(subject);
mail.setHtmlBody('Here is the email you requested! Check the attachment!');
set<Id> AccountIds = new set<Id> () ;
AccountIds = 'ACCOUNTID';
transient Messaging.EmailFileAttachment [] efaList = new List<Messaging.EmailFileAttachment> () ;
if(attachmentIds != null) {
for (Attachment attach1 : [select Name, Body, ContentType from Attachment where Id in :attachmentIds]) {
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(attach1.Name);
efa.setBody(attach1.Body);
efa.setContentType(attach1.ContentType);
efaList.add(efa) ;
}
}
if(emailAttachmentList != null) {
for (EmailAttachment emAttach : emailAttachmentList) {
if (emAttach.isChecked && emAttach.title !='Call Report PDF') {
attachmentIds.add(emAttach.attachmentId);
}
}
}
transient Messaging.EmailFileAttachment [] efaList = new List<Messaging.EmailFileAttachment> () ;
efaList.add(attach);
if(attachmentIds != null) {
for (Attachment attach1 : [select Name, Body, ContentType from Attachment where Id in :attachmentIds]) {
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(attach1.Name);
efa.setBody(attach1.Body);
efa.setContentType(attach1.ContentType);
efaList.add(efa) ;
}
}
if(efaList != null) {
mail.setFileAttachments(efaList) ;
}
mail.setCharset('UTF-8'); //Use Signatue
mail.setUseSignature(true);
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'EMAIL SENT WITH ATTACHMENT SUCCESSFULLY!!!'));
return null;
}

public list<string> convertToList(set<string> setToConvert) {
list<string> lstString = new list<string>();
lstString.addAll(setToConvert);
return lstString;
}

(3587)

Previous
Next

Leave a Reply