java - 如何将图像嵌入到模板 HTML Velocity 中?

标签 java html email velocity

如何将图像嵌入到 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

最佳答案

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

例如在我的模板中:

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

关于java - 如何将图像嵌入到模板 HTML Velocity 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33331341/

相关文章:

java - Bukkit ConfigurationSection getKeys

javascript - 音频节点实时输入-找不到声音不断更新的麻烦

html - 如何使用电子邮件处理媒体查询/非内联样式。

email - Grails发送HTML Mail签名的方法错误

PHP UTF8 邮件在 iPhone 邮件应用程序上出现乱码

java - Java中的 "the result to the type expected by the invoker"是什么?

java - 从文件系统中获取文件并将其作为 File 对象传递

java - Android 动态行创建中的 IllegalStateException

html - 如何在我的标题框中获取所有内容并将标题居中(资源)

javascript - 具有 rowspan 和 colspan 的响应表