MimeMessage 电子邮件不会发送附件
MimeMessage email wont send with attachments
所以我有一个电子邮件模板,可以完美地发送没有附件的表单。一直在尝试将附件(从临时存储在文件夹中的服务器中获取它们)添加到要通过电子邮件发送的表单中。
我正在使用 Mail.java class、emailService.java class 和表单的控制器来实现此功能。
我收到一个模糊的错误 "java null pointer exception"。知道为什么我的消息不会发送吗?我认为这与代码中添加的 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)...
如果可以的话,我会把它添加为评论(代表低于 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);
}
}
您需要使用:
@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);
}
所以我有一个电子邮件模板,可以完美地发送没有附件的表单。一直在尝试将附件(从临时存储在文件夹中的服务器中获取它们)添加到要通过电子邮件发送的表单中。
我正在使用 Mail.java class、emailService.java class 和表单的控制器来实现此功能。
我收到一个模糊的错误 "java null pointer exception"。知道为什么我的消息不会发送吗?我认为这与代码中添加的 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)...
如果可以的话,我会把它添加为评论(代表低于 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);
}
}
您需要使用:
@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);
}