gpt4 book ai didi

java - MimeMessage 电子邮件不会发送附件

转载 作者:行者123 更新时间:2023-11-30 10:08:57 26 4
gpt4 key购买 nike

所以我有一个电子邮件模板,可以完美地发送没有附件的表单。一直在尝试将附件(从临时存储在文件夹中的服务器中获取它们)添加到要通过电子邮件发送的表单中。

我正在使用 Mail.java 类、emailService.java 类和表单的 Controller 来实现此功能。

我收到一个模糊的错误“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);
}
}

Controller 中的发送函数:

@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);
}
}

关于java - MimeMessage 电子邮件不会发送附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53526870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com