如何将图像嵌入模板HTML Velocity?

时间:2015-10-25 15:20:03

标签: java html email velocity

如何将图像嵌入到速度html模板中,以便收件人可以在不打开文件的情况下阅读它?

我按照以下说明操作: 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();
        }
    }
}

但收件人电子邮件的结果是两个附件the recipient's email

1 个答案:

答案 0 :(得分:2)

这是一个较老的问题,但我今天遇到了谷歌搜索如何做同样的事情。我使用了Commons Email并按照以下说明操作:https://commons.apache.org/proper/commons-email/userguide.html

e.g。在我的模板中:

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

然后在我的Java代码中:

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