javax.mail和附件的问题

时间:2018-12-10 16:22:29

标签: javamail

我想使用javax.mail发送电子邮件。

它适用于ubuntu 14.04和Java 1.8.0_121和tomcat7

使用ubuntu 18.04和Java 1.8.0_181和tomcat8时出现此错误:

  

由于:java.io.IOException:“ text / html” DataContentHandler   需要String对象,被赋予类型类的对象   javax.mail.internet.MimeMultipart

这是代码:

MimeMessage mex = new MimeMessage(session);
mex.setFrom(new InternetAddress(from));
mex.addRecipient(RecipientType.TO, new InternetAddress((String)((List)ccList).get(0)));
mex.setRecipients(RecipientType.BCC, from);
mex.setSubject(subject);

MimeMultipart var26 = new MimeMultipart();
MimeBodyPart attachBodyPart = new MimeBodyPart();
attachBodyPart.setText(messageBody);
var26.addBodyPart(attachBodyPart);
attachBodyPart = new MimeBodyPart();
byte[] data = baos.toByteArray();
new FileDataSource(fileName);
attachBodyPart.setDisposition("attachment");
attachBodyPart.setContent(data, "application/pdf");
attachBodyPart.setFileName(fileName);
var26.addBodyPart(attachBodyPart);
mex.setContent(var26, "text/html");
Transport transport = session.getTransport("smtp");
transport.connect(param.getSmtpHost(), from, pass);
transport.sendMessage(mex, mex.getAllRecipients());
transport.close();

你有什么主意吗?

1 个答案:

答案 0 :(得分:1)

很难相信相同的代码可以在任何地方使用。

更改

attachBodyPart.setText(messageBody);

attachBodyPart.setText(messageBody, "html");

更改

mex.setContent(var26, "text/html");

mex.setContent(var26);