Smtp 如何从类路径在电子邮件velocity transformer模板中添加图像

Smtp 如何从类路径在电子邮件velocity transformer模板中添加图像,smtp,mule,html-email,velocity,mule-studio,Smtp,Mule,Html Email,Velocity,Mule Studio,我将Velocity Transformer电子邮件模板用于我的Mule smtp。有什么方法可以从我的类路径在电子邮件模板中添加图像吗? 例如。。如果我的类路径中有一个图像,比如abc.png,我可以在velocity电子邮件模板中使用它吗,比如

我将Velocity Transformer电子邮件模板用于我的Mule smtp。有什么方法可以从我的类路径在电子邮件模板中添加图像吗?
例如。。如果我的类路径中有一个图像,比如abc.png,我可以在velocity电子邮件模板中使用它吗,比如 您可以使用类路径资源作为源,将出站附件添加到Mule消息中。这些Mule邮件附件将由SMTP出站转换器转换为MIME部分

从这里的讨论来看,您似乎需要这样声明图像:

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

您必须在cid:之后使用唯一的ID,该ID与Content-ID部分标题一致。Mule允许您通过添加名为attachmentName+“headers”(attachmentName是出站附件的名称)的出站消息属性java.util.Map来指定自定义部件头


一个潜在的困难是
ObjectToMimeMessage
转换器中的代码负责转换
javax.activation.DataHandler
(来自Mule消息出站附件)在
javax.mail.BodyPart中
只调用
setFileName
,而不调用
setDisposition
,我认为这是图像正确显示所必需的。也就是说,我不是这里的专家,您可能知道更多关于正确生成带有附加图像的MIME电子邮件的信息。

我按照您的代码在velocity transformer中添加图像路径,如下所示,字符串徽标将从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);
        }
    }
}
公共最终类MessageTransformer扩展了AbstractMessageTransformer
{
私人VelocityEngine VelocityEngine;
私有字符串模板名;
私有模板;
//此部分用于通过为fileName和subscriberName声明setter和getter,从属性文件中获取值
私人字符串标志;
公共字符串getLogo(){
返回标志;
}  
公共标识(字符串标识){
this.logo=logo;
}
//此部分用于从mule flow中配置的类路径获取电子邮件模板
公共速度信息变压器()
{
registerSourceType(Object.class);
setReturnDataType(新的SimpleDataType(String.class));
}
公共无效设置速度引擎(最终速度引擎速度引擎)
{
this.velocityEngine=velocityEngine;
}
public void setTemplateName(最终字符串templateName)
{
this.templateName=templateName;
}
@凌驾
public void initialise()引发InitialisationException
{
尝试
{
template=velocityEngine.getTemplate(templateName);
}
捕获(最终异常e)
{
抛出新的InitialisationException(e,this);
}
}
@凌驾
公共对象transformMessage(最终多消息消息,最终字符串输出编码)引发TransformerException
{
尝试
{
最终StringWriter结果=新建StringWriter();
FileDataSource myFile=newfiledatasource(new File(logo));//它包含图像文件的路径
message.setOutboundProperty(“logo”,myFile);
// -------------------------------------------------------
最终映射上下文=新HashMap();
context.put(“message”,message);
put(“payload”,message.getPayload());
context.put(“logo”、message.getOutboundProperty(“logo”);
template.merge(新VelocityContext(context),result);//合并所有属性
System.out.println(“带模板的邮件发送成功!!!”;
System.out.println(result.toString());
返回result.toString();
}
捕获(最终异常e)
{
抛出新TransformerException(
MessageFactory.createStaticMessage(“无法使用模板转换消息:“+template”)
,e);
}
}
}
1)嵌入HTML中编码的图像Base64 e、 g

使用以下站点将图像转换为base64:

你好,大卫,我不想将图像附加到。。。。我希望将图像嵌入邮件中,如何在Velocity模板中实现这一点?这是使用MIME主体部分完成的,该部分是从Mule中的出站附件创建的。我使用带有Velocity模板的Mule,能够发送MIME类型text/html的邮件;字符集=UTF-8。。。我可以在模板中设置html代码,并可以查看html格式的邮件。。。但是我如何从我的类路径在模板中嵌入图像???比如的内容?是的,我使用了上述代码。在我的模板中,我有。。。其中$logo是包含图像路径的变量。。。。但是没有用。。。我的图像在模板文件夹下的资源文件夹中,我也保存了我的.vm模板。如果我添加了数据,请您澄清如何在src中添加图像路径:image/png;base64,sdafdsff23f