gpt4 book ai didi

java - MimeMessageHelper 无法使用 JavaMail 发送同一文件的两个附件?

转载 作者:行者123 更新时间:2023-12-01 16:15:00 24 4
gpt4 key购买 nike

正如标题所示,我无法发送同一文件的两个单独附件,另一方面,发送两个或多个不同附件(不同文件)没有问题。我从 Android 设备接收 newMessage(使用改造),附件内容以 byte[] 形式发送。

这里是代码:

public boolean sendNewMessage(Message newMessage, int idAccount) {

boolean messageSent = true;

Account acc = accountRepository.findById(idAccount).get();

boolean isAuthenticationRequired = acc.isAuthentication();


JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
//Properties props = mailSender.getJavaMailProperties();
Properties props = new Properties();
//SimpleMailMessage message = new SimpleMailMessage();

mailSender.setHost(acc.getSmtpAddress());
mailSender.setPort(acc.getSmtpPort()); //465 ...

mailSender.setUsername(acc.getUsername());
mailSender.setPassword(acc.getPassword());

props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.ssl.trust", acc.getSmtpAddress());

if (isAuthenticationRequired) {
props.put("mail.smtp.auth", "true");
} else {

}

mailSender.setJavaMailProperties(props);

MimeMessage mimeMessage = mailSender.createMimeMessage();
boolean hasAttachments = false;
if (newMessage.getAttachments().size() > 0) hasAttachments = true;
MimeMessageHelper helper = null;

try {

helper = new MimeMessageHelper(mimeMessage, hasAttachments);

if (hasAttachments){

for (Attachment att : newMessage.getAttachments()){

helper.addAttachment(att.getName(), new ByteArrayDataSource(att.getData(), createMimeType(att)));//I've also tried to attache current date_time to att.getName(), but it didn't work
}
}

helper.setFrom(newMessage.getFrom());
helper.setTo(newMessage.getTo().stream().toArray(String[]::new));
helper.setCc(newMessage.getCc().stream().toArray(String[]::new));
helper.setBcc(newMessage.getBcc().stream().toArray(String[]::new));

helper.setSubject(newMessage.getSubject());
helper.setText(newMessage.getContent());

mailSender.send(mimeMessage);

newMessage.setDate_time(LocalDateTime.now());
newMessage.setAccount(acc);
addNewMessage(newMessage); //adds to database

} catch (MessagingException e) {
e.printStackTrace();
messageSent = false;

} catch (Exception e) {
e.printStackTrace();
messageSent = false;
}

return messageSent;

}


private String createMimeType(Attachment att){

return URLConnection.guessContentTypeFromName(att.getName()+"."+att.getMime_type());
}

最佳答案

Java Mail 调试正在记录多个文件上传和多个内容部分,但在接收端可能只有一个,因为附件名称相同(无法找到证据,但 addAttachment 方法的 javadoc 表明某些内部工作已经到位)。我建议更改附件名称,即添加版本控制:

helper.addAttachment("temp.csv", new ByteArrayDataSource(new FileInputStream("temp.csv").readAllBytes(), "text/plain"));
helper.addAttachment("temp1.csv", new ByteArrayDataSource(new FileInputStream("temp.csv").readAllBytes(), "text/plain"));

关于java - MimeMessageHelper 无法使用 JavaMail 发送同一文件的两个附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62424594/

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