1

I have test code for my mail sending method. Sending only with html work fine, but I try to add attachment (local file, so "file://..."), and i get that. :

java.lang.NullPointerException at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)

My code :

@Inject
private JavaMailSender mailSender;

...

MimeMessage message = mailSender.createMimeMessage();
Multipart multipart = new MimeMultipart();

// html
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText(SomeHtml, "text/html; charset=\"UTF-8\"");
multipart.addBodyPart(htmlPart);

// image
MimeBodyPart imageBodyPart = new MimeBodyPart();
String fileName = ClassLoader.getSystemResource("chat1.jpg").toString();
DataSource source = new FileDataSource(fileName);
imageBodyPart.setDataHandler(new DataHandler(source));
imageBodyPart.setFileName(fileName);
multipart.addBodyPart(imageBodyPart);

message.setContent(multipart);
mailSender.send(message);

I've tried to use MimeMessageHelper, but i've got same result. I've tried source.getInputStream().close(), i've got java.io.FileNotFoundException (but i can find my image when i paste path into browser or explorer)

I don't understand, Any idea ? Thanks !

Couturier Boris
  • 198
  • 1
  • 10

1 Answers1

0

Ok, i juste change my code like here Null Pointer Exception while sending mail with attachment through JavaMail API on amazon server in Java Application

It's work, but i don't understand the real difference...

Community
  • 1
  • 1
Couturier Boris
  • 198
  • 1
  • 10