当我尝试发送一个附件时,无论是 zip 文件还是文本文件,代码都可以正常工作,但是当我尝试发送两个附件时,一个是文本文件,另一个是 Zip 文件,它会抛出错误。我尝试了多种方法,但仍然没有运气。
MimeMessage msg = new MimeMessage(session){
msg.addHeader("Content-type", directMessage.getBody().getContentType());
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(fromaddress));
msg.setSubject(directMessage.getSubject(), "UTF-8");
MimeMultipart mp = new MimeMultipart();
BodyPart bodyPart = null;
for(int i=0;i<directMessage.getAttachments().size();i++){
bodyPart = new MimeBodyPart();
bodyPart.setContent(Base64.decodeBase64(directMessage.getAttachments().get(i).getContent().getBytes()),directMessage.getAttachments().get(i).getContentType());
bodyPart.setDisposition(decryptedMessage.getBodyPart(0).getDisposition());
bodyPart.setFileName(directMessage.getAttachments().get(i).getFilename());
mp.addBodyPart(bodyPart);
mp.setSubType("multipart/mixed");
}
msg.setContent(mp);
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toaddress, false));
Transport.send(msg);
例外:
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: "text/plain" DataContentHandler requires String object, was given object of type class [B
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1167)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)