Android : Adding attachment in android using Java Mail

on Saturday, March 28, 2015


I am trying to add attachments to an email which I send using JavaMail. I can send the email without attachment but I get an error when I try to attach a file.



03-28 18:07:36.735: E/SendMail(6703): null
03-28 18:07:36.735: E/SendMail(6703): java.lang.NullPointerException
03-28 18:07:36.735: E/SendMail(6703): at com.example.email.GMailSender.addAttachment(GMailSender.java:95)
03-28 18:07:36.735: E/SendMail(6703): at com.example.email.MainActivity$1$1.run(MainActivity.java:28)


Here is the method I use to attach a file.



public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
message.setContent(_multipart);
}


and I call it in the Main Activity.



sender.addAttachment("/storage/extSdCard/DCIM/Camera/Photo.jpg");


Do you think there is something wrong with the path of the file, if so how can I get the correct path.


0 comments:

Post a Comment