如何从类路径中的电子邮件速度转换器模板中添加图像 [英] how to add image in email velocity transformer templates from classpath

查看:99
本文介绍了如何从类路径中的电子邮件速度转换器模板中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Mule smtp中使用了Velocity Transformer电子邮件模板.有什么方法可以从类路径中的电子邮件模板中添加图像? 举例来说,..如果我在类路径中有一个说abc.png的图像,我可以在<图片src = ......

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= ......

推荐答案

我已经按照以下代码按照以下方式在速度转换器中添加了图像路径,"String"徽标将从弹簧豆中获取值

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);
        }
    }
}

这篇关于如何从类路径中的电子邮件速度转换器模板中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆