我正在使用Velule Transformer电子邮件模板和我的Mule smtp。有没有什么方法可以从我的类路径中添加电子邮件模板中的图像? 那是例如..如果我在我的类路径中有一个图像说abc.png,我能否在我的速度电子邮件模板中使用它,如< image src = ......
答案 0 :(得分:1)
您可以使用类路径资源作为源来向Mule消息添加出站附件。这些Mule消息附件将由SMTP出站转换器转换为MIME部分。
从这里的讨论Embedding images into html email with java mail看来你需要声明这样的图像:
<img src=\"cid:uniqueImageID\"/>
您必须在cid之后使用唯一ID:这与Content-ID部件标头一致。 Mule允许您通过添加名为attachmentName +“Headers”的出站消息属性java.util.Map来指定自定义部件标头(attachmentName是出站附件的名称)。
一个潜在的难点是,ObjectToMimeMessage
变换器中的代码只需调用javax.activation.DataHandler
来转换javax.mail.BodyPart
(来自Mule消息出站附件)setFileName
1}}但不是setDisposition
我认为需要正确显示图像。这就是说,我不是这里的专家,您可能更了解如何正确生成带有附加图像的MIME电子邮件。
答案 1 :(得分:1)
1)在HTML中嵌入编码的Base64图像 例如
使用以下网站将图片转换为base64: http://www.dailycoding.com/Utils/Converter/ImageToBase64.aspx
答案 2 :(得分:0)
我已按照您的代码以下列方式在速度变换器中添加图像路径,String徽标将从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);
}
}
}