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
Vishal_ThoriyaVishal_Thoriya 

how Send an Email with any kind of file attachment to desired email address?

hi all,

 

i have question ?

 

how can we send an email with our file attachment  to desired email address ?

 

like gmail and yahoo i want the same kind of page......

 

any kind of help will be gratly appriciated..........

 

thanks in advance........

 

Vishal Thoriya.

Best Answer chosen by Admin (Salesforce Developers) 

All Answers

Vishal_ThoriyaVishal_Thoriya
public class EmailCardListController {
    //Did the email send successfully?
    public Boolean emailSent { get; set; }
    //Create the email handler;
    public Messaging.SingleEmailMessage mailHandler = new Messaging.SingleEmailMessage();
    
    //The recipient
    String[] emailRecipient = new String[] {'sgs.sgs@marutitech.com'};
    public void sendMail() {
    
        //set the recipient
        mailHandler.setToAddresses(emailRecipient);
        
        //set the reply email address
        mailHandler.setReplyTo('gsgs.sgs@marutitech.com');
        
        //set the display name
        mailHandler.setSenderDisplayName('Vishal Thoriya');
        
        //set the subject
        mailHandler.setSubject('Card List');
        
        //set the template ID
        //mailHandler.setTemplateId('00X200000015XFL');
        
        mailHandler.setHtmlBody('This is a simple email from me');
        
        try {
            Messaging.sendEmail(new Messaging.Email[] { mailHandler });
            emailSent = true;
        }catch(EmailException e) {
            System.debug(e.getMessage());
            emailSent = false;
        }
    }   
}

 

 the above code is my controller

 

 

and my apex page code is:

 

<apex:page controller="EmailCardListController" action="{!sendMail}" title="Email Card List">
<apex:outputText value="{!emailSent}" />
</apex:page>

 but i am using <apex:input file> to upload my file but 

 

my question is how to send it that file to my email address

DannyK89DannyK89
This was selected as the best answer
Vishal_ThoriyaVishal_Thoriya

hi this is my class code:

public with sharing class DocumentEmailController {
    public String email { get; set; }
    public Attachment attachment;
    public String ContactId;
    public Contact cse;

    public DocumentEmailController(ApexPages.StandardController controller) {
        this.cse=(Contact)controller.getRecord();
        ContactId = cse.id;
    }

    public PageReference sendDoc() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        //set the reply email address
        mail.setReplyTo('vishal.thoriya@marutitech.com');
        
        //set the display name
        mail.setSenderDisplayName('Vishal Thoriya');
        
        //set the subject
        mail.setSubject('Test');
        
        mail.setHtmlBody('This is a simple email from me');
        
        mail.setUseSignature(false);
        mail.setToAddresses(new String[] { email });
        mail.setWhatId(ContactId);
        
        //mail.setTemplateId('template id which you want to use');
        
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name,Body,BodyLength from Attachment where ParentId = :ContactId]) 
        {  // Add to attachment file list  
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
            efa.setFileName(a.Name); 
            efa.setBody(a.body); 
            fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Attachment sent to '+email));   
    
        return null;
    }
    
    public String getContactId() {
        return this.ContactId;  
    }  
    public void setContactId(String id) {  
       this.ContactId = id;  
    }

       
}

 

i am passing the contact id in my VF page url but i am recieving the email without attachment..........

 

i have no idea what is going wrong?

 

can you suggest me the issue?

 

why am not getting attachment 

 

the contactId which i am passing has two attachment files.........

 

Thanks in advance........

DannyK89DannyK89

Well I used your code on a test page and it seemed to work. 

 

I created a Detail page button and the Url is : /apex/testPage?Id={!Contact.Id}

 

The Visual Force Page:

 

<apex:page standardController="Contact" extensions="DocumentEmailController">
    <apex:form >
        <apex:inputText value="{!email}"/>
        <apex:commandButton value="Send Doc." action="{!sendDoc}"/>
        <apex:outputText value="{!ContactId}"/>
    </apex:form>
</apex:page>

 

Extensions :

 

public with sharing class DocumentEmailController {
    public String email { get; set; }
    public Attachment attachment;
    public String ContactId;
    public Contact cse;

    public DocumentEmailController(ApexPages.StandardController controller) {
        this.cse=(Contact)controller.getRecord();
        ContactId = cse.id;
    }

    public PageReference sendDoc() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        //set the reply email address
        mail.setReplyTo('vishal.thoriya@marutitech.com');
        
        //set the display name
        mail.setSenderDisplayName('Vishal Thoriya');
        
        //set the subject
        mail.setSubject('Test');
        
        mail.setHtmlBody('This is a simple email from me');
        
        mail.setUseSignature(false);
        mail.setToAddresses(new String[] { email });
        
        //mail.setTemplateId('template id which you want to use');
        
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name,Body,BodyLength from Attachment where ParentId = :ContactId]) 
        {  // Add to attachment file list  
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
            efa.setFileName(a.Name); 
            efa.setBody(a.body); 
            fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Attachment sent to '+email));   
    
        return null;
    }
    
    public String getContactId() {
        return this.ContactId;  
    }  
    public void setContactId(String id) {  
       this.ContactId = id;  
    }

       
}