Skip to content Skip to sidebar Skip to footer

How To Add Image In Email Velocity Transformer Templates From Classpath

I am using Velocity Transformer email template with my Mule smtp. Is there any ways that I can add images in the email templates from my classpath ? That is for example .. if I hav

Solution 1:

You can add outbound attachments to the Mule Message, using classpath resources as their source. These Mule Message attachments will be turned into MIME parts by the SMTP outbound transformer.

From the discussion here Embedding images into html email with java mail it seems you need to declare the images like this:

<img src=\"cid:uniqueImageID\"/>

You have to use a unique ID after cid: that is consistent with the Content-ID part header. Mule allows you to specify custom part headers by adding an outbound message property java.util.Map named attachmentName+"Headers" (attachmentName is the name of the outbound attachment).

One potential difficulty is that the code in the ObjectToMimeMessage transformer that takes care of transforming a the javax.activation.DataHandler (coming from the Mule Message outbound attachment) in a javax.mail.BodyPart only calls setFileName but not setDisposition which I think is needed for the image to show properly. This said, I'm not an expert here, you probably know more about properly generating MIME emails with attached images.

Solution 2:

1) Embed the image Base64 encoded in your HTML e.g.

Use following site to convert image to base64: http://www.dailycoding.com/Utils/Converter/ImageToBase64.aspx

Solution 3:

I had followed your code to add image path in the velocity transformer in the following way, the String logo will get the value from spring beans

publicfinalclassMessageTransformerextendsAbstractMessageTransformer
{
    private VelocityEngine velocityEngine;
    private String         templateName;
    private Template       template;

   //This part is for getting the value from property file by declaring setter and getter for fileName and  subscriberNameprivate String logo;
    public String getLogo() {
        return logo;
    }  
    publicvoidsetLogo(String logo) {
        this.logo = logo;
    }

    //This part is for getting template for email from classpath configured in mule flowpublicVelocityMessageTransformer()
    {
        registerSourceType(Object.class);
        setReturnDataType(newSimpleDataType<String>(String.class));
    }

    publicvoidsetVelocityEngine(final VelocityEngine velocityEngine)
    {
        this.velocityEngine = velocityEngine;
    }

    publicvoidsetTemplateName(final String templateName)
    {
        this.templateName = templateName;
    }

    @Overridepublicvoidinitialise()throws InitialisationException
    {
        try
        {
            template = velocityEngine.getTemplate(templateName);
        }
        catch (final Exception e)
        {
            thrownewInitialisationException(e, this);
        }
    }

    @Overridepublic Object transformMessage(final MuleMessage message, final String outputEncoding)throws TransformerException
    {


        try
        {
            finalStringWriterresult=newStringWriter();
            FileDataSourcemyFile=newFileDataSource (newFile (logo)); // It contains path of image file
            message.setOutboundProperty("logo", myFile);
            // -------------------------------------------------------final Map<String, Object> context = newHashMap<String, Object>();
            context.put("message", message);
            context.put("payload", message.getPayload());
            context.put("logo", message.getOutboundProperty("logo"));
            template.merge(newVelocityContext(context), result); //Merging all the attributes
            System.out.println("MAIL WITH TEMPLATE SEND SUCCESSFULLY !!!");
            System.out.println( result.toString() );
            return result.toString();               
        }
        catch (final Exception e)
        {
            thrownewTransformerException(
                           MessageFactory.createStaticMessage("Can not transform message with template: " + template)
                      , e);
        }
    }
}

Post a Comment for "How To Add Image In Email Velocity Transformer Templates From Classpath"