Create a SendingEmailVF visualforce Page
<apex:page standardController="Account" extensions="SendemailController">
<apex:form >
<script type="text/javascript">
function init() {
sendEmail();
}
if(window.addEventListener)
window.addEventListener('load',init,true)
else
window.attachEvent('onload',init)
</script>
<apex:actionFunction name="sendEmail" action="{!sendEmailFunction}">
</apex:actionFunction>
</apex:form>
</apex:page>
Controller
public class SendemailController {
public String AccountId {get;set;}
Public SendemailController(ApexPages.StandardController controller){
AccountId = ApexPages.currentPage().getParameters().get('Id');
}
Public Pagereference sendEmailFunction(){
String toaddress = 'test@test.com'; //your email address
try {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {toaddress};
mail.setToAddresses(toAddresses);
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 = :AccountId]){
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('https://na35.salesforce.com/'+AccountId);
reference.setRedirect(true);
return reference;
}
}
Create a Account Custom Button
OnClick JavaScript
window.location = 'apex/SendingEmailVF?id={!Account.Id}';