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
Ajay Ghuge 6Ajay Ghuge 6 

Get attachments from EmailMessage

Hi Folks,

I need to get the attachments from EmailMessage. Is there any way to get it ? EmailMessage object has a property called hasattachments but how to get attachments and its body ?

Regards,
Ajay
Best Answer chosen by Ajay Ghuge 6
Ajay Ghuge 6Ajay Ghuge 6
string strHTMLEmailBody = email.htmlBody;
string strEmailBody = email.htmlBody.replaceAll('<br/>', '\n');
strEmailBody = strEmailBody.replaceAll('<br />', '\n');
string HtmlPattern = '<.*?>';
Matcher match =  Pattern.compile(HtmlPattern).matcher(strEmailBody);
strHTMLEmailBody.stripHtmlTags();

All Answers

Narveer SinghNarveer Singh
Hi Ajay,

Below is the sample code to send Attachement with some message in Mail :
 
global  class Email {
    
    WebService static String sendEmail(String empid) {                 

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        PageReference pdf = Page.myPage;
        pdf.getParameters().put('id',empid);
        pdf.setRedirect(true);
 
        Blob bfile = pdf.getContent();

        Messaging.EmailFileAttachment efa  = new Messaging.EmailFileAttachment();
        efa.setFileName('Attachment.pdf');
        efa.setBody(bfile);
        String addresses;
        email.setSubject( 'My Subject ' );
        String[] test = new String[] {'abc@gmail.com'};
        email.setToAddresses(test);
        email.setSaveAsActivity(false);
        String messageBody = '<html>Hello </br>' + 
                
           ' <p>Task has been created with below details: </p><br> ' + 
           ' <p>Priority- </p></br> ' + 
           ' <p>Base Date- </p> '+        
        ' </html>';
        email.setHtmlBody(messageBody); 
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        // Send it
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});    
        
        return empid;
      }    
} 

you can call this class from any Button as below :

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 
var result = sforce.apex.execute("Email","sendEmail",{empid:"{!Contact.Id}"}); 
window.location.reload();
Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution.Let me know if anything else.

Best Regards
Narveer

 
Ajay Ghuge 6Ajay Ghuge 6
Hi Narveer,

Thanks for you reply. But this is not what I am looking for. I am sending email from salesforce for example : Email Quote and I want to get attachment from that email.

I hope you got my point.

Regards,
Ajay
Ajay Ghuge 6Ajay Ghuge 6
string strHTMLEmailBody = email.htmlBody;
string strEmailBody = email.htmlBody.replaceAll('<br/>', '\n');
strEmailBody = strEmailBody.replaceAll('<br />', '\n');
string HtmlPattern = '<.*?>';
Matcher match =  Pattern.compile(HtmlPattern).matcher(strEmailBody);
strHTMLEmailBody.stripHtmlTags();
This was selected as the best answer
Aditya Patil 7Aditya Patil 7
Hello Ajay,

Were you able to figure out how to get attachments from the EmailMessage object?

Thank you.
Sincerely,
Aditya
ChellappaChellappa

hi Guys,
 

I am also looking exactly for the same. I want to be able to process incoming Email with attachments and add them to the EmailMessage record.
I am able to correctly create an Email Message but not able to add attachment to that record?
Was wondering if we have to use something like ContentDocument?
If any of you found something after the last comment on Oct 29, 2018, Please let me know.
Thanks,
Chellappa

ChellappaChellappa
Hi Guys,
I think i got the answer. We indeed have to create a Content Document record with all related records like Contentversion, ContentDocumentLink etc. 
Chellappa
Talha SaqibTalha Saqib
In Inbound Email messages, attachments are in the form of URLs, we can get attachments from URLs though.

For that, please try the following resource:
https://retrology.net/how-to-create-a-file-from-an-email-attachment-in-salesforce