function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek Kumar 407Abhishek Kumar 407 

Unable to send attachment with the click of button which will redirect to my email Id. I am getting subject and Body but not attachment. Kindly help here...

Here is the Apex and VF code for the reference:-

//Apex Code
 
public class sendEmailbuttonApex
{
    public Account accounts                     {set;get;}
    public String subject 				{set;get;}
    public String body 			        {set;get;}
    public blob Attach                                {set;get;}  

    public sendEmailbuttonApex(Apexpages.StandardController controller)
    {
        accounts=(Account)controller.getRecord();
        accounts=new Account(Name='Test');
    }
    public PageReference sendEmail()
    {
        List<Contact> conList=[Select Id, Email from contact where Email='abhishekk.twopirconsulting@gmail.com'];
        system.debug(conList);
        List<String> mail=new List<String>();
        for(Contact c:conList)
        {
            mail.add(c.Email);
        }
        Messaging.SingleEmailMessage msg1=new Messaging.SingleEmailMessage();
        msg1.setToAddresses(mail);
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        Messaging.sendEmail(emails);
        insert accounts;
        Blob b = Attach;
        Attachment At = new Attachment(Name ='NewFile',body = b,ParentId=accounts.Id);
        insert At; 
        
        PageReference p=new PageReference('https://mail.google.com/mail/u/0/#inbox');
        p.setRedirect(true);
        return p;
        
    }
}

//VF code
 
<apex:page docType="HTML-5.0" standardController="Account" extensions="sendEmailbuttonApex">
    <head lang="en">
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        </head>
        <body>
           <apex:form >
                <script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
     		<br /><br />
        <apex:outputLabel value="Subject" for="Subject"/>:<br />     
        <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
        <br /><br />
        <apex:outputLabel value="Body" for="word_count"/>:<br />     
        <apex:inputTextarea id="word_count" cols="30" rows="10" value="{!body}" />
        <br />
        Total word Count : <span id="display_count">0</span> words. Words left : <span id="word_left">40</span>
         <br /><br /><br />
        <apex:commandButton value="Send Email" action="{!sendEmail}" /> 
        <apex:inputFile value="{!Attach}"></apex:inputFile>
           </apex:form>
    	<script>
 			$(document).ready(function() {
			//   alert('Hello, jQuery');
            $('[id$=word_count]').on('keyup', function(e) {
                //  alert('hey');
                //j$('[id$=word_count]').on('keydown', function(e) {
                var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
                if (words <= 40) {
                   $('[id$=display_count]').text(words);
                   $('[id$=word_left]').text(40-words)
                }else{
                    if (e.which !== 8) e.preventDefault();
                }
            });
        }); 
        </script>
    </body>
</apex:page>

​ 
Best Answer chosen by Abhishek Kumar 407
GhanshyamChoudhariGhanshyamChoudhari
Messaging.SingleEmailMessage msg1=new Messaging.SingleEmailMessage();
        msg1.setToAddresses(mail);
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        //Messaging.sendEmail(emails);
        insert accounts;
        Blob b = Attach;
        Attachment At = new Attachment(Name ='NewFile',body = b,ParentId=accounts.Id);
        insert At;
		
		 List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :accounts.Id]){
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            fileAttachments.add(efa);
        }
        msg1.setFileAttachments(fileAttachments);
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { emails});

 

All Answers

GhanshyamChoudhariGhanshyamChoudhari
Messaging.SingleEmailMessage msg1=new Messaging.SingleEmailMessage();
        msg1.setToAddresses(mail);
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        //Messaging.sendEmail(emails);
        insert accounts;
        Blob b = Attach;
        Attachment At = new Attachment(Name ='NewFile',body = b,ParentId=accounts.Id);
        insert At;
		
		 List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :accounts.Id]){
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            fileAttachments.add(efa);
        }
        msg1.setFileAttachments(fileAttachments);
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { emails});

 
This was selected as the best answer
Abhishek Kumar 407Abhishek Kumar 407
Hi Sir,

I tried your code, but it's giving me error as I am new to salesforce, So unable to fix it. Thanks for your reply.

Error is: Initial expression is of incorrect type, expected: Messaging.SingleEmailMessage but was: List<Messaging.Email>
GhanshyamChoudhariGhanshyamChoudhari
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] { emails});
Messaging.sendEmail(emails);