Spring 如何发送带有附件的电子邮件
我想发送一封附有图片的电子邮件。我将Spring3与velocity模板一起使用。我可以做到这一点,但由于某些原因,当我添加一个扩展名与图像名称,我没有得到电子邮件送达 以下是我使用的代码:Spring 如何发送带有附件的电子邮件,spring,email,email-attachments,Spring,Email,Email Attachments,我想发送一封附有图片的电子邮件。我将Spring3与velocity模板一起使用。我可以做到这一点,但由于某些原因,当我添加一个扩展名与图像名称,我没有得到电子邮件送达 以下是我使用的代码: private MimeMessage createEmail(Application application, String templatePath, String subject, String toEmail, String fromEmail, String fromName) { M
private MimeMessage createEmail(Application application, String templatePath, String subject, String toEmail, String fromEmail, String fromName) {
MimeMessage mimeMsg = mailSender.createMimeMessage();
Map<String, Object> model = new HashMap<String, Object>();
model.put("application", application);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templatePath, model);
text = text.replaceAll("\n", "<br>");
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true);
helper.setSubject(subject);
helper.setTo(toEmail);
if (fromName == null) {
helper.setFrom(fromEmail);
} else {
try {
helper.setFrom(fromEmail, fromName);
} catch (UnsupportedEncodingException e) {
helper.setFrom(fromEmail);
}
}
helper.setSentDate(application.getDateCreated());
helper.setText(text, true);
InputStream inputStream = servletContext.getResourceAsStream("images/formstack1.jpg");
helper.addAttachment("formstack1", new ByteArrayResource(IOUtils.toByteArray(inputStream)));
} catch (MessagingException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
return mimeMsg;
}
private MimeMessage createEmail(应用程序、字符串模板路径、字符串主题、字符串到邮件、字符串fromEmail、字符串fromName){
MimeMessage mimeMsg=mailssender.createMimeMessage();
映射模型=新的HashMap();
模型。put(“应用程序”,应用程序);
String text=VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,templatePath,model);
text=text.replaceAll(“\n”和“
”);
试一试{
MimeMessageHelper=新的MimeMessageHelper(mimeMsg,true);
helper.setSubject(subject);
helper.setTo(toEmail);
if(fromName==null){
helper.setFrom(fromEmail);
}否则{
试一试{
setFrom(fromEmail,fromName);
}捕获(不支持的编码异常e){
helper.setFrom(fromEmail);
}
}
setEntDate(application.getDateCreated());
setText(text,true);
InputStream InputStream=servletContext.getResourceAsStream(“images/formstack1.jpg”);
addAttachment(“formstack1”,新的ByteArrayResource(IOUtils.toByteArray(inputStream));
}捕获(消息异常e){
抛出新的运行时异常(e);
}
捕获(IOE异常){
抛出新的运行时异常(e);
}
返回mimeMsg;
}
使用上面的代码,我可以添加formstack1作为附件,但它没有扩展名,因此我无法获得formstack1.jpg图像文件。但是当我在helper.addAttachment(“formstack1”,newbytearrayresource(IOUtils.toByteArray(inputStream))中使用formstack1.jpg作为要附加的资源名称时
由于formstack1改为formstack1.jpg
我甚至没有收到邮件。我正在使用smtp.gmail.com
和25
作为端口。不过,我确实在控制台上成功发送了电子邮件。但是电子邮件
永远不会交付
编辑:如果我像helper.addAttachment(“formstack1”,newbytearrayresource(IOUtils.toByteArray(inputStream))那样保存它
并将扩展名从nothing更改为.jpg,在下载附加图像时,我确实获得了所需的图像
有人能帮我理解为什么会这样,以及如何使用Spring3发送包含1个或多个附件的电子邮件吗
谢谢。您最好使用Apache Commons HtmlEMail
无法获取
commons-email-1.3
版本,maven存储库只有commons-email-1.1
最新版本。我错过什么了吗?是该文件的下载链接。看起来像是。jpg
即使将内容类型设置为image/jpeg
它也不起作用,图像也不符合规范。但是当我尝试使用.png
时,一切都正常。