我想使用java邮件发件人发送电子邮件。我有字节数组,我应该作为邮件附件发送。我尝试了多种方式,但即将推出的附件不是可读格式。它以字节为单位.Below是我试过的代码。
public void sendMailWithAttachments(EmailContent emailContent,
byte[] attachment) {
try{
MimeMessage mimeMessage = emailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new
MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setTo(emailContent.getTo());
mimeMessageHelper.setFrom(emailContent.getFrom());
mimeMessageHelper.setCc(emailContent.getCc());
mimeMessageHelper.setSubject(emailContent.getSubject());
final InputStreamSource attachmentSource = new ByteArrayResource(attachment);
DataSource dataSource = new ByteArrayDataSource(attachment, "application/pdf");
mimeMessageHelper.addAttachment("testattachment", dataSource);
emailSender.send(mimeMessageHelper.getMimeMessage());
emailContent.setStatus(true);
}catch(Exception e){
e.printstacktrace
}