Send Email With Document As An Attachment Using Apex In Salesforce

Sample Code Approach 1:

//Get your document from document Object
Document doc = [Select Id, Name, Body, ContentType, DeveloperName, Type From Document Where DeveloperName = 'Your_Doucment_DeveloperName'];

//Create Email file attachment from document
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType(doc.ContentType);
attach.setFileName(doc.DeveloperName+'.'+doc.Type);
attach.setInline(false);
attach.Body = doc.Body;

//Apex Single email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { 'itzbiswajeet@gmail.com' });//Set To Email Address
mail.setSubject('Test Email With Attachment');//Set Subject
mail.setHtmlBody('Please find the attachment.');//Set HTML Body
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });//Set File Attachment
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });//Send Email

Sample Code Approach 2:

//Get your document from document Object
Document doc = [Select Id, Name, Body, ContentType, DeveloperName, Type From Document Where DeveloperName = 'Your_Doucment_DeveloperName'];

//Apex Single email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { 'itzbiswajeet@gmail.com' });//Set To Email Address
mail.setSubject('Test Email With Attachment');//Set Subject
mail.setHtmlBody('Please find the attachment.');//Set HTML Body
mail.setDocumentAttachments(new Id[]{doc.Id});//Set Document Attachment
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });//Send Email