+ Start a Discussion
bhanu_prakashbhanu_prakash 

share content file via email on click on button

Hi Team,
I have content file in my org 
ContentDocument doc = [SELECT Id,Title,Description FROM ContentDocument WHERE ID= '06911000000eZPPA2']
i need to share that file as attachment in email on click on button. i have designed vf page and class. I can able to share document but not in Content file. Help me to design it.

Apex Class
public class sendEmail {
    
    public sendEmail(ApexPages.Standardcontroller controller){
    }

    public String subject {get; set;}
    public String body {get; set;}
    public blob attbody {get; set;}
    public String attname {get; set;}
    public String sendTo {get; set;}
    public String oppList {get; set;}
    public list<ContentVersion> docList {get; set;}
    private final Opportunity Opp;
    public blob file { get; set; }
    public ContentVersion ConVer { get; set; }

    // Constructor to get Opportunity data

    public sendEmail(){
        Opp = [SELECT Id, Name,(SELECT Contact.Name,Contact.Email,Contact.Id,Contact.AccountId,ContactId,Role, Contact.Account.Name 
                     FROM OpportunityContactRoles) FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id')
            
        ];
        docList = new list<ContentVersion>();
        EmailTemplate ev = [Select id, name,Subject,Body from EmailTemplate];
        subject = 'Test Subject';
        body = 'Test body';
    }
	 public Opportunity getOpp() {
        return Opp;
    }
    
    public PageReference send(){
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        EmailTemplate ev = [Select id, name,Subject, Body from EmailTemplate where name = 'Test Template'];
        String addresses;     

        String[] toAddresses = addresses.split(':',0);
        // Sets the paramaters of the email
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( body );
        

        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('attname');
        efa.setBody(attbody);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        // Sends the email
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
		
        return null;
    }
    
    public String  docName {get; set;}
    public PageReference uploadPDF(){
        /*
        Document d = new Document();
        d.folderid = UserInfo.getUserId();
        d.name = this.attname;
        d.body = this.attbody;
		*/
        ContentVersion d = [SELECT Id,Title,Description FROM ContentDocument WHERE ID= '0690I0000097dNe'];
        d.versionData = file;
        d.title = 'from VF';
        d.pathOnClient ='/foo.txt';
        try {
            insert d;
            docList.add(d);
          //  docName = d.name;
           docName = d.title;
            send();
        }
        catch(exception e){
            system.debug('Error:'+e);
            apexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'e-'+e));
        }
        finally{
            attbody = null;
          //  d = new Document();
        }
        return null;
    }

}

Visualforce Page:
<!---- Created to Send Email from Button on Opportunity -->

<apex:page standardController="Opportunity" extensions="sendEmail" sidebar="false" showHeader="false">
    <apex:messages />
    <apex:form id="form">
        <apex:pageblock >
            <apex:pageBlockSection title="Send Email" Columns="1">
                 <div class= "Lookup">
                <apex:outputLabel value="Send To:"/>
                <apex:inputText value="{!sendTo}" id="sendTo"/>
               
                    <a href="#" id="link1" title="Lookup" tabindex="6" >
                        <img class="lookupIcon" title="Lookup" src="/s.gif" style="vertical-align:bottom;"/>
                    </a>
                </div>
                
                <apex:outputLabel value="Subject:" for="Subject"/>
                <!--  <apex:inputText value="{!subject}" id="Subject" maxlength="80"/> -->
                <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
                
                <apex:outputLabel value="Body:" for="Body"/>
                <apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/>
                
                <apex:inputFile value="{!attbody}" filename="{!attname}"/>
                <apex:commandButton value="Attach and Send" action="{!uploadPDF}"/>
                
                <apex:outputPanel id="PanelId">
                    <table>
                        <apex:repeat value="{!doclist}" var="str">
                            <tr>
                                <td>{!str.name}
                                </td>
                            </tr>
                        </apex:repeat>
                    </table>
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
    <style>
    .lookup{
        position: relative;
        left: 181px;
        top: -30px;
        
        }
    </style>
</apex:page>
I have checked muliplte questions in forum, community and sites nothing helps me.

Thanks