嘗試用某些正文文本發送帶有Excel附件的電子郵件。 無論出於何種原因,我收到的電子郵件始終是一封電子郵件,其中包含我想作爲附件發送的電子郵件,然後將Excel本身附加到附加的電子郵件中...爲什麼我的電子郵件方法始終以電子郵件附件的形式發送電子郵件?
是否有某些屬性禁用此行爲?
public void sendEmailWithAttachment(EmailProperties properties) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
message.setFrom(properties.getFrom());
message.setHeader("X-Mailer", properties.getHeader());
if (properties.getPriority() != null) {
message.setHeader("X-Priority", properties.getPriority().getValue());
}
InternetAddress[] toEmailArray = properties.getToAddresses().toArray(new InternetAddress[properties.getToAddresses().size()]);
message.setRecipients(javax.mail.Message.RecipientType.TO, toEmailArray);
message.setSubject(properties.getSubject());
message.setSentDate(new Date());
Multipart multipart = new MimeMultipart();
if (StringUtils.isNotBlank(properties.getMessageText())) {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setText(properties.getMessageText());
multipart.addBodyPart(mimeBodyPart);
}
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(properties.getContents(), properties.getContentType())));
mimeBodyPart.setFileName(properties.getFileName());
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
send(message);
} catch (Exception exc) {
logger.error("Error attempting to send email!", exc);
}
}
您是否嘗試僅發送附件或文本以檢查消息的存在方式發送?此外,爲了使代碼更易於閱讀,請嘗試重命名正文部分變量(例如:textBodyPart和fileBodyPart)。它有助於更好地理解代碼。 –