因此,我有一个电子邮件模板,可以完美地发送不带附件的表格。一直试图将附件(从临时存储在文件夹中的服务器上获取附件)添加到要通过电子邮件发送的表单中。
我正在使用Mail.java类,emailService.java类以及表单的控制器来实现此功能。
我收到一个模糊的错误“ java空指针异常”。知道为什么我的消息不会发送吗?我认为这与fileSystemResource除了代码有关。但是在我的控制台中,它显示了文件...
电子邮件服务代码:
@Service
public class EmailService {
private JavaMailSender javaMailSender;
@Autowired
public EmailService(JavaMailSender javaMailSender){
this.javaMailSender = javaMailSender;
}
@Autowired
public StorageService storage;
@Autowired
private SpringTemplateEngine templateEngine;
public void sendSimpleMessage(Mail mail, DirectBind directBind) throws MessagingException, IOException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,
MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
StandardCharsets.UTF_8.name());
helper.addAttachment("Mail_Icon.png", new ClassPathResource("static/images/Mail_Icon.png"));
Context context = new Context();
context.setVariables(mail.getModel());
context.setVariable("directBind",directBind);
String html = templateEngine.process("emailMessage", context);
helper.setTo(mail.getTo());
helper.setText(html, true);
helper.setSubject(mail.getSubject());
helper.setFrom(mail.getFrom());
// List<Object> files = new ArrayList<>();
// files.add(storage.getUploadDir());
FileSystemResource allFiles= new FileSystemResource(new File(storage.getUploadDir()));
helper.addAttachment(allFiles.getFilename(),allFiles);
javaMailSender.send(message);
}
}
在控制器中发送功能
@RequestMapping(value="/directBind", params="send")
public String send(Model model, @ModelAttribute(value="directBind") DirectBind directBind){
List<String> businessAgencyList = directBind.getBusinessAgencyList();
Mail mail = new Mail();
mail.setFrom("no-reply@hgitservices.com");
mail.setTo(new String[]{"stacief@hgitservices.com"});
mail.setSubject("Oli Affiliate - AMS360 & PMA Data Checklist");
Map<String, Object> mailModel = new HashMap<String, Object>();
mail.setModel(mailModel);
// List<Object> files = new ArrayList<>();
// files.add(storageService.getUploadDir());
// mail.setAttachments(files);
try {
emailService.sendSimpleMessage(mail, directBind);
} catch (Exception e) {
e.printStackTrace();
return ("redirect:/?sentMessageFail");
}
return ("redirect:/?sentMessage");
}
@RequestMapping(value="/email")
public String email(){
return "emailMessage";
}
}
我的邮件模型:
public class Mail {
private String from;
private String[] to;
private String subject;
private List<Object> attachments;
private Map<String, Object> model;
public Mail() {
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String[] getTo() {
return to;
}
public void setTo(String[] to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public List<Object> getAttachments() {
return attachments;
}
public void setAttachments(List<Object> attachments) {
this.attachments = attachments;
}
public Map<String, Object> getModel() {
return model;
}
public void setModel(Map<String, Object> model) {
this.model = model;
}
}
错误消息...
org.springframework.mail.MailSendException: Failed messages: java.lang.NullPointerException; message exception details (1) are:
Failed message 1:
java.lang.NullPointerException
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)...
答案 0 :(得分:1)
如果可以的话,我会将其添加为评论(代表在50以下)。
您可以尝试替换
helper.addAttachment(allFiles.getFilename(),allFiles);
与
helper.addAttachment(allFiles.getFilename(),allFiles.getFile());
编辑:
FileSystemResource allFiles= new FileSystemResource(new File(storage.getUploadDir() + "/filename.txt"));
遍历文件(可能需要调整):
File myDir = new File(storage.getUploadDir());
File[] myFiles = myDir.listFiles();
if (myFiles != null) {
for (File cFile : myFiles) {
helper.addAttachment(cFile.getName(), cFile);
}
}
答案 1 :(得分:0)
您需要使用:
@Override
public void sendAttachmentMessage(List<String> to, String subject, String text, String pathToAttachment) {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setTo(to.toArray(new String[to.size()]));
helper.setSubject(subject);
String content = builder.build(text);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(pathToAttachment));
helper.addAttachment(file.getFilename(), file);
} catch (MessagingException e) {
LOG.error("Error sending email with attachment.", e);
}
emailSender.send(message);
}