Public with sharing class SendemailController{ public String caseId {get;set;} Public SendemailController(){ caseId = ApexPages.currentPage().getParameters().get(‘Id’); } Public Pagereference sendEmailFunction(){ Case getEmail = [SELECT Id, Contact.Email FROM Case WHERE id=:caseId]; if(getEmail.Contact.Email != null) { String toaddress = getEmail.Contact.Email; try { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {toaddress}; String[] ccAddresses = new String[] {‘test@gmail.com’}; mail.setToAddresses(toAddresses); mail.setCcAddresses(ccAddresses); mail.setReplyTo(toaddress); mail.setSenderDisplayName(‘Name’); mail.setSubject(‘Testing email through apex’); mail.setBccSender(false); mail.setUseSignature(true); mail.setPlainTextBody(‘This is test email body. This mail is being sent from apex code’); //mail.setHtmlBody(‘<b> This is HTML body </b>’ ); List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :caseId]){ Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); efa.setFileName(a.Name); efa.setBody(a.Body); fileAttachments.add(efa); //mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); } mail.setFileAttachments(fileAttachments); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } catch(Exception e) {} } PageReference reference = new PageReference(‘http://new-developer-edition.ap1.force.com/ThankYou?caseId=’+caseId); reference.setRedirect(true); return reference; } }
Ajax
(1)
Apex Class
(12)
Apex Trigger
(2)
Community
(2)
Home Page
(1)
HTML
(4)
Integration
(3)
JS
(7)
KB
(1)
Label
(1)
Licenses
(1)
Listing
(1)
Log
(1)
OOPs
(5)
Sharing
(1)
Static Resource
(1)
Test Class
(3)
URI
(1)
Visualforce
(10)
Sunday 2 March 2014
Sending Email with Attachment using Apex & Visualforce page
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment