如何将图像嵌入模板HTML Velocity? [英] How to embed images into template HTML Velocity?

查看:234
本文介绍了如何将图像嵌入模板HTML Velocity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将图像嵌入速度html模板中,以便收件人无需打开文件即可阅读它?

How to embed images into velocity html template so the recipient can read it without opening the file?

我正在按照以下说明操作:
http://sengopal.github.io/blog/blog/java -mail-using-velocity-templates.html

I am following the instructions at: http://sengopal.github.io/blog/blog/java-mail-using-velocity-templates.html

这是我的代码:

public class SendEmailVelocity {
    public static void main(String[] args) {

        final String username = Contants.myEmail;
        final String password = Contants.myPass;
        final String toMailRecipient = Contants.mailRecipient;

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {

            Template template = Velocity.getTemplate("helloworld.vm");
            VelocityContext context = new VelocityContext();
            context.put("name", "ABC123");
            StringWriter message = new StringWriter();
            template.merge(context, message);


            MimeMultipart multipart = new MimeMultipart("related");

            BodyPart imageBodyPart = new MimeBodyPart();

            StringBuffer imgPath = new StringBuffer().append(File.separator).append("D:\\Temp\\data\\logo.gif");

            DataSource fds = new FileDataSource(imgPath.toString());
            imageBodyPart.setDataHandler(new DataHandler(fds));

            imageBodyPart.setHeader("Content-ID", "<imageID>");

            multipart.addBodyPart(imageBodyPart);

            BodyPart messageBodyPart = new MimeBodyPart();

            StringBuffer messageBuffer = new StringBuffer();
            messageBuffer.append(message.toString());
            messageBuffer.append("<img src='cid:imageID\'>");

            messageBodyPart.setContent(messageBuffer.toString(), "text/html");
            multipart.addBodyPart(messageBodyPart);

            Message msg = new MimeMessage(session);

            msg.setContent(multipart);
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(toMailRecipient));

            msg.setSubject("Test Email Velocity ");
            msg.setSentDate(new Date());
            msg.setFrom(new InternetAddress("dummy@example.com"));

            Transport transport = session.getTransport("smtp");
            transport.connect(props.getProperty("connectHost"), props.getProperty("connectUser"), props.getProperty("connectPassword"));

            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
            System.out.println("Sent!!!");

        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

但是收件人的结果电子邮件是两个附件收件人的电子邮件

But the result of the recipient's email is two attachments the recipient's email

推荐答案

这是一个较旧的问题,但今天我在Google上搜索了如何做同样的事情。我使用了Commons Email并遵循以下说明: https://commons.apache.org/正确/commons-email/userguide.html

This is an older question, but I came across it today Googling how to do the same thing. I used Commons Email and followed these instructions: https://commons.apache.org/proper/commons-email/userguide.html

例如在我的模板中:

<img src="cid:${cid}" alt="Logo">

然后在我的Java代码中:

And then in my Java code:

URL url = new URL("something.png");
String cid = email.embed(url, "Logo");
Map emailModel = new HashMap();
emailModel.put("cid", cid);

这篇关于如何将图像嵌入模板HTML Velocity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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