sending attachments from android

Observations from an outlier

sending attachments from android


Here is a simple class to send yourself a gmail from your Arndoid phone. You’ll need mail.jar in your eclipse project, but the rest is just this one class below. We are using this to test on the chair bot (OpenPAW). It is very useful to send all the debug files right to your email box. Further, the zephyropen framework can send data directly to the cloud on event too, but need to be careful about bandwidth when away from wifi.

More info, examples on

Call the send mail function from Activity class like this:

if (!BackupToServer.sendFileViaFTP(context)) { new Mail("FTP backup failed", mailBody).send(); }

This class requires you to manage your own passwords, be careful with them!

import java.util.Date; import java.util.Properties; import javax.activation.CommandMap; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.activation.MailcapCommandMap; import javax.mail.BodyPart; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import android.util.Log; /**  *   * uses this jar:  *  *   * @author [email protected]  *   */ public class Mail extends javax.mail.Authenticator { private static final String PORT = "465"; private static final String PORTS = "465"; private static final String HOST = ""; private static final String USER = "xxxx"; private static final String PASS = "xxxx"; private static final String[] TO = { USER }; private static final String FROM = USER; private Multipart multipart = new MimeMultipart(); private String subject = "subject"; private String body = "body text"; public Mail(String sub, String txt) { subject = sub; body = txt; MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); CommandMap.setDefaultCommandMap(mc); } public void send() { final Properties props = setProperties(); final Session session = Session.getInstance(props, this); new Thread() { public void run() { try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(FROM)); InternetAddress[] addressTo = new InternetAddress[TO.length]; for (int i = 0; i < TO.length; i++) addressTo[i] = new InternetAddress(TO[i]); msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); msg.setSubject(subject); msg.setSentDate(new Date()); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(body); multipart.addBodyPart(messageBodyPart); // Put parts in message msg.setContent(multipart); // send email Transport.send(msg); Log.e("MAIL", "message sent"); } catch (Exception e) { Log.e("MAIL", "fail to send:" + e.getMessage()); } } }.start(); } private Properties setProperties() { Properties props = new Properties(); // TODO: enable debugging here // props.put("mail.debug", "false"); props.put("", HOST); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", PORT); props.put("mail.smtp.socketFactory.port", PORTS); props.put("mail.smtp.socketFactory.class", ""); props.put("mail.smtp.socketFactory.fallback", "false"); return props; } 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); } @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USER, PASS); } }