我正在尝试使用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());
}
是正确的,甚至正确修改了速度模板。那么这可能导致失败的原因是什么?非常感谢。
答案 0 :(得分:1)
是的,谢谢你的建议。后来发现问题......只是这部分
<div id="paragraph2>
<img src='cid:${photo2}' width="200px" height="200px" style="display: block;float: right; margin: 0em 0em 1em 1em "/>
<p>${paragraph2}
由于我从未关闭过引号,因此图像从未显示过。我的错,非常抱歉,再次感谢您的回复。