【问题标题】:JavaMailSenderImpl sending inline photosJavaMailSenderImpl 发送内嵌照片
【发布时间】:2011-03-21 17:26:18
【问题描述】:

我正在尝试使用 Spring 的邮件实现发送电子邮件,并使用速度模板来替换 html 文件的内容。到目前为止,它工作得很好,但是现在我在尝试向要发送的邮件中添加第二个内联图像时遇到了麻烦。

我的速度模板是这个:

<html>
<head>
    <title>Ndeveloper publishing</title>
</head>
<body>
    <div id="header" style="background-color: #eeeeee">
        <div align="center">
            <p><em>Header1</em></p>
        </div>
    </div>
    <div id="content">
        <div id="paragraph1">
            <img src='cid:${photo1}' width="200px" height="200px" style="display: block;float: left; margin: 0em 1em 1em 0em "/>
            <p>${paragraph1}
            </p>
        </div>
        <div id="paragraph2>
            <img src='cid:${photo2}' width="200px" height="200px" style="display: block;float: right; margin: 0em 0em 1em 1em "/>
            <p>${paragraph2}
            </p>
        </div>
    </div>
    <div id="footer"  style="background-color: #eeeeee">
        <div align="center">
            <p><em>Footer1</em></p>
        </div>
    </div>
</body>

现在我用来发送邮件的代码如下所示:

@SuppressWarnings("unchecked")
public void sendTemplateMail(VelocityMailMessage message) {
    Connection connection = null;
    Session session = null;


    try {
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Velocity.init(initializeVelocityProperties());
        VelocityContext velocityContext = new VelocityContext();

        HashMap<String, Object> parameterMap=message.getReplaceableParameters();
        HashMap<String, Attachment> attachmentMap=message.getAttachList();

        //${paragraph1} and ${paragraph2} are replaced here
         for (String key : parameterMap.keySet()) {
            velocityContext.put(key, parameterMap.get(key));
        }
        //Here the inline photos identifiers should be replaced ${photo1} and ${photo2}
        int k=1;
        for (String key: attachmentMap.keySet())
        {
            //INLINE_PHOTO_PREFIX has a value of "photo"               
            velocityContext.put(Constants.INLINE_PHOTO_PREFIX+k, attachmentMap.get(key).getIdentifier());
            k++;
        }

        StringWriter text = new StringWriter();
        Velocity.mergeTemplate(message.getTemplateName(), "UTF-8", velocityContext, text);

        List<String> emailList = message.getTo();

        ArrayList<String> emails = new ArrayList<String>();
        for (Iterator<String> iterator = emailList.iterator(); iterator
                .hasNext();) {
            String[] tmp = null;
            String[] tmp1 = null;
            int i = 0;
            int j = 0;
            String name = (String) iterator.next();
            tmp = name.split(";");
            while (i < tmp.length) {
                tmp1 = tmp[i].split(",");
                i++;
                j = 0;
                while (j < tmp1.length) {
                    emails.add(tmp1[j]);
                    j++;
                }
            }

        }
        if (!emails.isEmpty()) {
            emailList = emails;
        }

        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        MimeMessage mimeMessage = sender.createMimeMessage();
        String[] toArray = new String[emailList.size()];
        int i = 0;
        for (String to : emailList) {
            toArray[i] = to;
            i++;
        }


            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

            helper.setText(text.toString(), true);
            helper.setTo(toArray);
            helper.setFrom(message.getFrom(), "Portal");
            helper.setReplyTo(message.getFrom());
            helper.setSubject(message.getSubject());
            if (message.getAttachList() != null) {
                if (!(message.getAttachList().isEmpty())) {
                    Set<String> keys = message.getAttachList().keySet();
                    for (String string : keys) {
                            Attachment at=message.getAttachList().get(string);
                            if(at.isInline()){
                                helper.addInline(at.getIdentifier(), at.getAttachFile());
                            }else{
                                helper.addAttachment(string, message.getAttachList()
                                .get(string).getAttachFile());
                            }
                    }
                }
            }
            sender.setHost(parameterServiceLocal.parameterByName("SMTP HOST")
                    .getValue());
             sender.setUsername(parameterServiceLocal.parameterByName("SMTP USER").getValue());
             sender.setPassword(parameterServiceLocal.parameterByName("SMTP PASSWORD").getValue());
            Properties p = new Properties();

            p.put("mail.smtp.starttls.enable","true");
            p.put("mail.smtp.auth", "true");
            sender.setJavaMailProperties(p);
            sender.send(mimeMessage);

    } catch (VelocityException e) {
        e.printStackTrace();
    }  catch (MessagingException e) {
        e.getMessage(); 
    }
    catch (JMSException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null && session != null) {
            try {
                session.close();
                connection.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }

Constants.INLINE_PHOTO_PREFIX 是简单的字符串“photo”,用于替换 vecloity 模板中的值。

问题在于,当您查看发送到收件箱的邮件时,它只显示了 ${photo1} 符号所在的第一张照片。我已经检查过了,所有参数都达到了

if(at.isInline()){
          helper.addInline(at.getIdentifier(), at.getAttachFile());
}

是正确的,即使速度模板被正确修改。那么失败的可能原因是什么?非常感谢。

【问题讨论】:

  • 该代码很难阅读。你需要把它分解成更小的方法,这样我们才能理解它。首先将速度的东西与邮件的东西分开。

标签: spring jakarta-mail velocity


【解决方案1】:

是的,谢谢你的建议。后来发现问题....只是这部分

<div id="paragraph2>
        <img src='cid:${photo2}' width="200px" height="200px" style="display: block;float: right; margin: 0em 0em 1em 1em "/>
        <p>${paragraph2}

由于我从未关闭引号,因此图像从未显示。我的错,真的很抱歉,再次感谢您的回复。

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 2020-05-06
    • 1970-01-01
    • 2011-12-21
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2021-10-03
    相关资源
    最近更新 更多