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
wixxeywixxey 

Sending a Static Resource image in Email

Hello!
I want to send a Static resource image in Email through a controller. the code which i have written for it is given below

Public void sendMail(){
     
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[] {info.Location__r.Account__r.Email__c}; 
         mail.setToAddresses(toAddresses);
         mail.setSenderDisplayName('Support');
         mail.setSubject('Invoice');
         mail.setBccSender(false); 
         mail.setUseSignature(false);
         mail.setHtmlBody('<image src="' + StaticResourceURL.GetResourceURL('cpLogo')+ '"/>'); 
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); }

 and the StaticResourcUrl is a class which i have search on net for getting the url of a static resource

public class StaticResourceURL
{
    //Pass the resource name
    public static String GetResourceURL(String resourceName)
    {
        //Fetching the resource
        List<StaticResource> resourceList = [SELECT Name, NamespacePrefix, SystemModStamp FROM StaticResource WHERE Name = :resourceName];
                            
        //Checking if the result is returned or not
        if(resourceList.size() == 1)
        {
           //Getting namespace
           String namespace = resourceList[0].NamespacePrefix;
           //Resource URL
           
           return '/resource/' + resourceList[0].SystemModStamp.getTime() + '/' + (namespace != null && namespace != '' ? namespace + '__' : '') + resourceName; 
        }
        else return '';
    }
    
    public static testMethod void testStaticResourceURL(){
    	StaticResourceURL.GetResourceURL('cpLogo');
    	System.debug(StaticResourceURL.GetResourceURL('cpLogo'));
    }
    
}

 

but this code is not working for me although system.debug in the class returns /resource/1368709494000/cpLogo ... kindly guide me what is wrong with it

Best Answer chosen by Admin (Salesforce Developers) 
wixxeywixxey

Finally I got help from the blog http://www.shellblack.com/salesforce/sales-cloud/html-email/

Instead of storing the image in static resource i stored it in document, I just removed the controller class, and find the url of the image and pass it into the setHtmlBody of mail in the img-tag, and it works for me

All Answers

Amritesh SinghAmritesh Singh

Hi,

 

Your code is right,but  as i know you can't send static resouces like that.  

http://help.salesforce.com/apex/HTViewSolution?id=000005925&language=en_US

http://help.salesforce.com/apex/HTViewSolution?id=000003730&language=en_US

 

better save image in document and send document image through email

 

 

Amritesh

wixxeywixxey

Finally I got help from the blog http://www.shellblack.com/salesforce/sales-cloud/html-email/

Instead of storing the image in static resource i stored it in document, I just removed the controller class, and find the url of the image and pass it into the setHtmlBody of mail in the img-tag, and it works for me

This was selected as the best answer
Katia HageKatia Hage

By the way, there are also instructions for referencing an image in an HTML email template from a document in this help topic: https://help.salesforce.com/HTViewHelpDoc?id=creating_custom_html_email_templates.htm&language=en_US

Chitral ChaddaChitral Chadda

 if i send image (storing it in documnet)  and copying link in img tag.  in  htmlbody
the image in not visible if i check thru laptop , but wen i check on cellphome the iamge is being displayed