includes some of the problems and solution which i face while developing it.
Friday, 28 February 2014
Tuesday, 11 February 2014
Send email with attachment from Apex class and VF Page in salesforce.
Hi friends,
This post shows how to send Email with Attachment using Apex Controller and VF Page.
I am writing this post because it’s a common requirement to sends emails from salesforce with attachment to clients, contacts etc.
As per my knowledge there are two ways to send email from Apex:-
1.
By using email templates.
2.
By using Messaging class.
Here I am using Messaging class for sending email from apex class:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="EmailComposePageController" sidebar="false"> | |
<apex:form style="width: 70%; margin-left: 15%;"> | |
<apex:pageMessages ></apex:pageMessages> | |
<apex:pageBlock title="Email"> | |
TO::<apex:inputText style="width:100%" value="{!emailVal}"/><br> | |
CC::<apex:inputText style="width:100%" value="{!cc}"/> </br><br> | |
Subject::<apex:inputText style="width:100%" value="{!emailSubject}"/></br><br> | |
Attachment::<apex:inputFile value="{!att.body}" fileName="{!att.name}"/> | |
<apex:commandButton title="Upload" action="{!upload}" value="Upload"/><BR/> | |
<apex:repeat value="{!attList}" var="att"> | |
{!att.Name}<br/> | |
</apex:repeat> | |
<br/>BODY::</br><br> | |
<apex:inputTextarea style="height:300px; width:100%" value="{!body}"/></br> | |
<apex:commandButton value="Send Mail" action="{!sendMail}"/> | |
</apex:pageBlock> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailComposePageController { | |
public String emailVal{get;set;} | |
public String cc{get;set;} | |
public String emailSubject{get;set;} | |
public String body{get;set;} | |
public Attachment att{get;set;} | |
public List<Attachment> attList{get;set;} | |
private List<String> emailIds; | |
private List<String> ccEmailIds; | |
// simple Constructor for creting object of Attachment list and Attachment. | |
public EmailComposePageController(){ | |
attList = new List<Attachment>(); | |
att = new Attachment(); | |
} | |
// method to attach attachment to attachment list. | |
public void upload(){ | |
if(att.name == null){ | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Select File Path First.')); | |
} | |
else{ | |
att.OwnerId = UserInfo.getUserId(); | |
attList.add(att); | |
att = new Attachment(); | |
} | |
} | |
// this action method is calls from send button. | |
public void sendMail() { | |
if( emailVal !='' && emailVal.length() > 0 ) | |
emailIds = emailVal.split(';'); | |
if( cc !='' && cc.length() > 0 ) | |
ccEmailIds = cc.split(';'); | |
Messaging.SingleEmailMessage mail = sendEmail(emailIds ,ccEmailIds ,null, emailSubject, body); | |
if(mail != null){ | |
Messaging.SendEmailResult [] r =Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); | |
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Email Has Been Sent'); | |
ApexPages.addMessage(myMsg); | |
} | |
} | |
// from this method the actual mail is sends. | |
private Messaging.SingleEmailMessage sendEmail( List<String> toMail, List<String> ccEmailIdList,List<String> bccEmailIdList, String strSubject, String strMsgBody ){ | |
List<Messaging.EmailFileAttachment> mailAttList = new List<Messaging.EmailFileAttachment>(); | |
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); | |
email.setToAddresses(toMail); | |
if(ccEmailIdList != null && ccEmailIdList.size()>0){ | |
email.setCcAddresses(ccEmailIdList); | |
} | |
if(bccEmailIdList != null && bccEmailIdList.size()>0){ | |
email.setBccAddresses(bccEmailIdList); | |
email.setBccSender(true); | |
} | |
email.setSubject(strSubject); | |
email.setPlainTextBody(strMsgBody); | |
email.setSaveAsActivity(false); | |
for(Attachment att1 : attList){ | |
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); | |
efa.setBody(att1.body); | |
efa.setFileName(att1.name); | |
mailAttList.add(efa); | |
} | |
email.setFileAttachments(mailAttList); | |
Messaging.SendEmailResult[] sendResult; | |
return email ; | |
} | |
} |
Screen shots:-
hope it helps, happy coding...
Subscribe to:
Posts (Atom)