Send mail to multiple mail Ids with PDF as attachment.
I am sending mail with pdf of account record by providing Id of account and I define a page 'AccountRecord' to attach in attachment and use render page as pdf in visualforce page.
I create a list of email Ids, But you can use any collections of email Ids list.
I am sending mail with pdf of account record by providing Id of account and I define a page 'AccountRecord' to attach in attachment and use render page as pdf in visualforce page.
I create a list of email Ids, But you can use any collections of email Ids list.
Apex Class:
public class sendMail
{
public sendMail()
{
List<String> emails=new List<String>();
String accountId='001280000058S3M';
emails.add('arunvictor93@gmail.com');
emails.add('arun.garg@girikon.com');
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
PageReference pageName=Page.AccountRecord;
pageName.getParameters().put('id',accountId);
pageName.setRedirect(true);
Blob b;
// Take the PDF content
if (Test.IsRunningTest())
{
b=Blob.valueOf('UNIT.TEST');
}
else
{
b= pageName.getContent();
}
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('account'+'.pdf');
efa.setBody(b);
List<String> toAddresses = new list<string>();
toAddresses = emails;
// Sets the paramaters of the email
email.setToAddresses(toAddresses);
email.setSubject('Valley Force Upcoming Order');
email.setHtmlBody('Hi, This is message with attachment. ');
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
if (results[0].success)
{
System.debug('The email was sent successfully.');
} else
{
System.debug('The email failed to send: ' + results[0].errors[0].message);
}
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent with Attachment!'));
}
}
This visualforce page is used for attachment in mail and getting account Id as a parameter in apex class and display data.
Visualforce Page(AccountRecord):
<apex:page controller="AccountData" renderAs="pdf">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!lstAcc}" var="acc">
<apex:column value="{!acc.name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class(AccountData):
public class AccountData
{
public List<Account> lstAcc{get;set;}
public AccountData()
{
String accId=apexpages.currentpage().getparameters().get('id');
if(String.isNotBlank(accId))
{
lstAcc=[Select id,name from Account where id=:accId];
}
}
}
0 comments:
Post a Comment
If you have any doubts, please let me know.