how to add image in email velocity transformer tem

2019-09-03 02:23发布

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 have an image say abc.png in my classpath, can I able to use it in my velocity email template like < image src= ......

3条回答
唯我独甜
2楼-- · 2019-09-03 03:01

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

public final class MessageTransformer extends AbstractMessageTransformer
{
    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  subscriberName

    private String logo;
    public String getLogo() {
        return logo;
    }  
    public void setLogo(String logo) {
        this.logo = logo;
    }

    //This part is for getting template for email from classpath configured in mule flow
    public VelocityMessageTransformer()
    {
        registerSourceType(Object.class);
        setReturnDataType(new SimpleDataType<String>(String.class));
    }

    public void setVelocityEngine(final VelocityEngine velocityEngine)
    {
        this.velocityEngine = velocityEngine;
    }

    public void setTemplateName(final String templateName)
    {
        this.templateName = templateName;
    }

    @Override
    public void initialise() throws InitialisationException
    {
        try
        {
            template = velocityEngine.getTemplate(templateName);
        }
        catch (final Exception e)
        {
            throw new InitialisationException(e, this);
        }
    }

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


        try
        {
            final StringWriter result = new StringWriter();
            FileDataSource myFile = new FileDataSource (new File (logo)); // It contains path of image file
            message.setOutboundProperty("logo", myFile);
            // -------------------------------------------------------

            final Map<String, Object> context = new HashMap<String, Object>();
            context.put("message", message);
            context.put("payload", message.getPayload());
            context.put("logo", message.getOutboundProperty("logo"));
            template.merge(new VelocityContext(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)
        {
            throw new TransformerException(
                           MessageFactory.createStaticMessage("Can not transform message with template: " + template)
                      , e);
        }
    }
}
查看更多
smile是对你的礼貌
3楼-- · 2019-09-03 03:14

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.

查看更多
贪生不怕死
4楼-- · 2019-09-03 03:19

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

查看更多
登录 后发表回答