0

I'm trying to attach a file of SD with GmailSender in Android.

I have this: Sending email with attachment through GMailSender?

I checked that the file exists, but the mail won't send correctly. The same method without a file attached runs ok.

What could be the problem be?

Thx.

File imageFile = new File("/sdcard/prueba.jpg");
if (imageFile.exists()){
    Toast.makeText(getApplicationContext(),"Existe",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"NO Existe", Toast.LENGTH_LONG).show();
}
gmail.sendMail("Datos", c.getMailBody(),Constants.getAddress(), udb.getMail(), imageFile);

My GmailSender class:

    public class GMailSender extends javax.mail.Authenticator{

    private final static String defaultUserName = "particle";
    private final static String defaultPassword = "particle123";

    private String mailhost = "smtp.gmail.com";   
    private String user;   
    private String password;   
    private Session session;   

    static {   
        Security.addProvider(new JSSEProvider());   
    }  

    public GMailSender() {   
        new GMailSender(defaultUserName, defaultPassword);
    }

    public GMailSender(String user, String password) {   
        this.user = user;   
        this.password = password;   

        Properties props = new Properties();   
        props.setProperty("mail.transport.protocol", "smtp");   
        props.setProperty("mail.host", mailhost);   
        props.put("mail.smtp.auth", "true");   
        props.put("mail.smtp.port", "465");   
        props.put("mail.smtp.socketFactory.port", "465");   
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");   
        props.put("mail.smtp.socketFactory.fallback", "false");   
        props.setProperty("mail.smtp.quitwait", "false");   

        session = Session.getDefaultInstance(props, this);   
    }   

    protected PasswordAuthentication getPasswordAuthentication() {   
        return new PasswordAuthentication(user, password);   
    }   

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
        try{
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
        message.setDataHandler(handler);   
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        Transport.send(message);   
        }catch(Exception e){

        }
    }  

    public synchronized void sendMail(String subject, String body, String sender, String recipients, File attachment) throws Exception { 
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
        message.setDataHandler(handler);
        if(attachment!=null){
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(body);
            MimeBodyPart mbp2 = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(attachment);
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            mp.addBodyPart(mbp2);
            message.setContent(mp);
        }
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        Transport.send(message);
    }


    public class ByteArrayDataSource implements DataSource {   
        private byte[] data;   
        private String type;   

        public ByteArrayDataSource(byte[] data, String type) {   
            super();   
            this.data = data;   
            this.type = type;   
        }   

        public ByteArrayDataSource(byte[] data) {   
            super();   
            this.data = data;   
        }   

        public void setType(String type) {   
            this.type = type;   
        }   

        public String getContentType() {   
            if (type == null)   
                return "application/octet-stream";   
            else  
                return type;   
        }   

        public InputStream getInputStream() throws IOException {   
            return new ByteArrayInputStream(data);   
        }   

        public String getName() {   
            return "ByteArrayDataSource";   
        }   

        public OutputStream getOutputStream() throws IOException {   
            throw new IOException("Not Supported");   
        }   
    }   
} 

My excepcion is:

javax.mail.MessagingException: IOException while sending message;

nested exception is: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_1_1079440400.1348225974758" at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1177) at javax.mail.Transport.send0(Transport.java:195) at javax.mail.Transport.send(Transport.java:124) at teru.SimDetect.TFC.GMailSender.sendMail(GMailSender.java:105) at teru.SimDetect.TFC.SimDetectActivity.onCreate(SimDetectActivity.java:56) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) at android.app.ActivityThread.access$1500(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:4385) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) at dalvik.system.NativeStart.main(Native Method) Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_1_1079440400.1348225974758" at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905) at javax.activation.DataHandler.writeTo(DataHandler.java:330) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1485) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1773) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1121) ... 17 more

Community
  • 1
  • 1

2 Answers2

0

Maybe this could help you:

String image = "prueba.jpg";
File file = new File(Environment.getExternalStorageDirectory(), image);
GMailSender sender = new GMailSender("user@gmail.com", "password");
sender.sendMail (head , body ,"user@gmail.com", "sendto@mail.com", file);

GmailSender modified:

public synchronized void sendMail(String subject, String body, String sender, String recipients, File attachment) throws Exception { 
    MimeMessage message = new MimeMessage(session);   
    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
    message.setSender(new InternetAddress(sender));   
    message.setSubject(subject);   
    message.setDataHandler(handler);
    if(attachment!=null){
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText(body);
        MimeBodyPart mbp2 = new MimeBodyPart();
        FileDataSource fds = new FileDataSource(attachment);
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(fds.getName());
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);
        message.setContent(mp);
    }
    if (recipients.indexOf(',') > 0)   
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
    else  
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
    Transport.send(message);
}   
lfergon
  • 963
  • 15
  • 27
  • No, i had doing this before, but it isnt run. IOException while sendind message. – user1686776 Sep 20 '12 at 18:18
  • Could you post the stacktrace? – lfergon Sep 20 '12 at 19:06
  • not, because if i'm connected with the cable, i can't aced to the SD memory. but in toast i get IOException while sendind message in Transport.send(message); – user1686776 Sep 20 '12 at 19:12
  • App works on the emulator? The same code that you have is working perfectly on my emulator, I connect without any kind of problems with gmail and I send mail. – lfergon Sep 21 '12 at 05:18
  • I`m not working on emulator because my application is very complex. I'm working on mobile with the cable. i don't understand why not working, it would be a small stupid thing. – user1686776 Sep 21 '12 at 07:59
  • it could be the route to attach the file?? – user1686776 Sep 21 '12 at 08:02
  • maybe, or one of the parameterst that you send to the function. Maybe some of them are incorrect, a comma, one point – lfergon Sep 21 '12 at 08:04
  • the parameters are the same that I use when i send mail without attach, and it run well. Anyway i check it. – user1686776 Sep 21 '12 at 08:15
  • Could be the route to the sdcard image, don´t get the correct kind of file and give problems when you try to send mail attachment – lfergon Sep 21 '12 at 14:49
  • I access to the file like you write in your asnwer and like in the post that you write. the file is in root of SD, and I check capitals/lowercase. file is called prueba.jpg i don't find the problem. – user1686776 Sep 21 '12 at 17:22
0

The code was well, The problem was the GMailSender libraries, it was obsolete.

Thanks you.