Tuesday, August 12, 2014

How to send mail on Android programatically (mail API)

There are two ways to do it, using intents or using a mail API.  The main difference is that when implementing an intent, you are basically leveraging a mail app that is already installed in your phone and therefore, this method is much simpler. Here I will explain how implement the mail API.


How to do it:

1. Download the java mail library: https://code.google.com/p/javamail-android/downloads/list
2. Add the corresponding permissions in the AndroidManifest.xml file:
 
   
3.Create a class MailHelper and copy the following code:
import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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;


public class MailHelper {

    private String originEmail;
    private String originPassword;

    private String destinationEmail;

    private String auth;
    private String sslEnable;
    private String smtpHost;
    private String smtpPort;

    public Mail(String originEmail,String originPassword,String destinationEmail){
        this.originEmail=originEmail;
        this.originPassword=originPassword;
        this.destinationEmail=destinationEmail;

        this.auth="true";
        this.sslEnable="true";
        this.smtpHost="smtp.gmail.com";
        this.smtpPort="587";
    }

    public boolean send(String attachmentFilePath){
        boolean noError=true;
        final String username=originEmail;
        final String password=originPassword;

        Properties props = new Properties();
        props.put("mail.smtp.auth", auth);
        props.put("mail.smtp.starttls.enable", sslEnable);
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", smtpPort);

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            BodyPart text=new MimeBodyPart();
            text.setText("Text");

            BodyPart bodyAttachment=new MimeBodyPart();
            bodyAttachment.setDataHandler(new DataHandler(new FileDataSource(attachmentFilePath)));
            bodyAttachment.setFileName("Attachment");

            MimeMultipart mm=new MimeMultipart();
            mm.addBodyPart(text);
            mm.addBodyPart(bodyAttachment);

            MimeMessage message=new MimeMessage(session);
            message.setFrom(new InternetAddress(originEmail));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(destinationEmail));
            message.setSubject("Subject");
            message.setContent(mm);

            Transport.send(message);
        } catch (MessagingException e) {
            noError=false;
            throw new RuntimeException(e);

        }
        return noError;
    }
}

4.Place the following code in a separate thread, because this a time consuming task that can potentially freeze your UI. You can usean AsynTask for this purpose, for example.
import java.io.File;

MailHelper mail=new MailHelper("origin@gmail.com","originPassword"
,"destination@gmail.com");
Boolean noError=m.send(fileAbsolutePath);










No comments:

Post a Comment