I know that this question is asked very often but I still don´t know how to fix my problem. I´m trying to send an email with an attachment. Although I tried several fixes which seem to work for other users I can´t get rid of this.
05-18 16:52:36.974: W/System.err(3580): javax.mail.MessagingException: IOException while sending message;
05-18 16:52:36.974: W/System.err(3580): nested exception is:
05-18 16:52:36.974: W/System.err(3580): javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
05-18 16:52:36.974: W/System.err(3580): boundary="----=_Part_0_1110436240.1400424754410"
05-18 16:52:36.974: W/System.err(3580): at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1244)
05-18 16:52:36.974: W/System.err(3580): at walla.dev.shareit.ShareTask.sendEmail(ShareTask.java:176)
05-18 16:52:36.974: W/System.err(3580): at walla.dev.shareit.ShareTask.doInBackground(ShareTask.java:37)
05-18 16:52:36.974: W/System.err(3580): at walla.dev.shareit.ShareTask.doInBackground(ShareTask.java:1)
05-18 16:52:36.974: W/System.err(3580): at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-18 16:52:36.974: W/System.err(3580): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-18 16:52:36.974: W/System.err(3580): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-18 16:52:36.974: W/System.err(3580): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-18 16:52:36.974: W/System.err(3580): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-18 16:52:36.974: W/System.err(3580): at java.lang.Thread.run(Thread.java:841)
05-18 16:52:36.974: W/System.err(3580): Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
05-18 16:52:36.974: W/System.err(3580): boundary="----=_Part_0_1110436240.1400424754410"
05-18 16:52:36.974: W/System.err(3580): at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905)
05-18 16:52:36.974: W/System.err(3580): at javax.activation.DataHandler.writeTo(DataHandler.java:330)
05-18 16:52:36.974: W/System.err(3580): at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1608)
05-18 16:52:36.974: W/System.err(3580): at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1849) 05-18 16:52:36.984: W/System.err(3580): at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1196) 05-18 16:52:36.984: W/System.err(3580): ... 9 more
This is my function:
private void sendEmail(String phonenumber,String...params) throws AddressException, MessagingException {
String host = "smtp.gmail.com";
String address = "********@gmail.com";
String from = "***********@gmail.com";
String pass = "***********";
String to="xxxxxxxx@gmail.com";
Multipart multiPart;
String finalString="";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", address);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Log.i("Check", "done pops");
Session session = Session.getDefaultInstance(props, null);
DataHandler handler=new DataHandler(new ByteArrayDataSource(finalString.getBytes(),"text/plain" ));
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setDataHandler(handler);
Log.i("Check", "done sessions");
multiPart=new MimeMultipart();
InternetAddress toAddress;
toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
Log.i("Check", "added recipient");
message.setSubject("Auto-Mail with pictures for the agency");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("This is an automatic message for used to share pictures to the agency attached the pictures. The Sharers phonenumber is:"+phonenumber);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
MimeBodyPart msgBodyPart = new MimeBodyPart();
//search File filename by uri
File filename=new File(params[1]);
DataSource source = new FileDataSource(filename);
//messageBodyPart.setDataHandler(new DataHandler(source));
msgBodyPart.setFileName(params[1]);
msgBodyPart.setHeader("Content-type", "image/jpeg");
//msgBodyPart.setDisposition();
try {
msgBodyPart.attachFile(new File(params[1]));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
multipart.addBodyPart(msgBodyPart);
message.setContent(multiPart);
//message.setText("This is an automatic message for used to share pictures to the agency attached the pictures. The Sharers phonenumber is:"+phonenumber);
Log.i("check", "transport");
Transport transport = session.getTransport("smtp");
Log.i("check", "connecting");
transport.connect(host,address , pass);
Log.i("check", "wana send");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Log.i("check", "sent");
I don´t know wether it´s important, I´ve downloaded the newest javax.mail.jar-version (1.5.2) from oracles webpage but the parts activation.jar and additional.jar are from googlecode, cause I don´t find them on the webpage.
If you need further informations please comment.