Sunday 18 September 2011

Android: Sending emails without user intervention (Using Javax Mail API).

Android API doesn't have support for any email protocols but the good news is that there's a javax implementation (a very good one by the way) here.
Download here these files:

  • mail.jar
  • additionnal.jar
  • activation.jar

and add them to your Android project as external jars. To do that in Eclipse, open the project properties (right click on the project name and click properties) and select Java Build Path. Click on the tab Libraries and them Add External JARs. Select the downloaded files and that's it.


Here's a simple example to programmatically send emails using SMTP with authentication but NO SSL/TLS:

public class MySMTPSendMail extends javax.mail.Authenticator {

  private String mUserName;
  private String mPassword;
  private String mHostName;

  public void sendEmailTo(String user, String password, String hostname, String recipient, String body)  throws AddressException, MessagingException{

    mUserName=user;
    mPassword=password;
    mHostName=hostname;

    Properties props = getProperties();


    // this object will handle the authentication
    Session session=Session.getInstance(props,this);
    MimeMessage emailMessage=new MimeMessage(session);
    BodyPart msgBody=new MimeBodyPart();
    MimeMultipart bodyMultipart=new MimeMultipart();


    emailMessage.setFrom(new InternetAddress(mUserName));
    emailMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recipient));  

    emailMessage.setSubject(body);    
    msgBody.setText(body);   
    bodyMultipart.addBodyPart(msgBody);
    emailMessage.setContent(bodyMultipart);

    Transport.send(emailMessage);
  }

  private Properties getProperties(){
    Properties props = new Properties(); 
   
    props.put("mail.smtp.host", mHostName);   
    props.put("mail.smtp.auth", "true");           
    // default SMTP port
    props.put("mail.smtp.port", "25"); 
  }

  @Override 
  public PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication(mUserName, mPassword); 
  } 
}

To send emails using SSL encryption, just change the method getProperties() to something like

  private Properties getProperties(){
    Properties props = new Properties(); 
   
    props.put("mail.smtp.host", mHostName);   
    props.put("mail.smtp.auth", "true");           
    // default SMTP over SSL port
    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"); 
  }


To send emails using TLS encryption (e.g, live.com) change the method getProperties() to

  private Properties getProperties(){
    Properties props = new Properties(); 
   
    props.put("mail.smtp.host", mHostName);   
    props.put("mail.smtp.auth", "true");           
    // default SMTP over SSL port
    props.put("mail.smtp.port", "465"); 
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.starttls.enable", "true");   
  }

Good luck!! ;)