thomas
thomas

Reputation: 1271

Issue with javax.mail and attached file

I want to send an email with javax.mail.

It works on ubuntu 14.04 and java 1.8.0_121 and tomcat7

With ubuntu 18.04 and java 1.8.0_181 and tomcat8 I am getting this error :

Caused by: java.io.IOException: "text/html" DataContentHandler requires String object, was given object of type class javax.mail.internet.MimeMultipart

Here is the code :

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();

Do you have any idea?

Upvotes: 0

Views: 178

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

It's hard to believe this same code works anywhere.

Change

attachBodyPart.setText(messageBody);

to

attachBodyPart.setText(messageBody, "html");

Change

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

to

mex.setContent(var26);

Upvotes: 1

Related Questions