JavaMailSenderImpl sending inline photos
I'm trying to send an email using Spring's mail implementation, and using velocity templates for replacing content of html files. So far it has worked great, but right now I'm facing trouble when trying to add a second inline image to the mail is gonna to be sent.
My velocity template is this one:
<html>
<head>
<title>Ndeveloper publishing</title>
</head>
<body>
<div id="header" style="background-col开发者_如何学JAVAor: #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>
Now the code that I'm using to send the mail looks like this:
@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();
}
}
}
Where Constants.INLINE_PHOTO_PREFIX
is simple the string "photo" used to replace the values in the vecloity template.
The problem is that when you check the mail sent to your inbox, it only shows the first photo where the ${photo1} symbol is. I already checked and all the parameters reaching
if(at.isInline()){
helper.addInline(at.getIdentifier(), at.getAttachFile());
}
are correct, even the velocity template is modified correctly. SO what would be a possible reason for this to fail?. Thanks a lot.
Yeah, thanks for the advice. Found the problem later.... it was just this part
<div id="paragraph2>
<img src='cid:${photo2}' width="200px" height="200px" style="display: block;float: right; margin: 0em 0em 1em 1em "/>
<p>${paragraph2}
Since I never closed the quotes the image was never displayed. My fault, really sorry and thanks again for the response.
精彩评论