All the code provided in the other answers is correct and is working fine, but a bit messy, so I decided to publish a library (still in development though) to use it in a easier way: AndroidMail.
You have just to create a MailSender, build a mail and send it (already handled in background with an AsyncTask).
MailSender mailSender = new MailSender(email, password);Mail.MailBuilder builder = new Mail.MailBuilder();Mail mail = builder .setSender(senderMail) .addRecipient(new Recipient(recipient)) .setText("Hello") .build();mailSender.sendMail(mail);
You can receive a notification for the email sent and it has also the support for different Recipients types (TO, CC and BCC), attachments and html:
MailSender mailSender = new MailSender(email, password);Mail.MailBuilder builder = new Mail.MailBuilder();Mail mail = builder .setSender(senderMail) .addRecipient(new Recipient(recipient)) .addRecipient(new Recipient(Recipient.TYPE.CC, recipientCC)) .setText("Hello") .setHtml("<h1 style=\"color:red;\">Hello</h1>") .addAttachment(new Attachment(filePath, fileName)) .build();mailSender.sendMail(mail, new MailSender.OnMailSentListener() { @Override public void onSuccess() { // mail sent! } @Override public void onError(Exception error) { // something bad happened :( }});
You can get it via Gradle or Maven:
compile 'it.enricocandino:androidmail:1.0.0-SNAPSHOT'
Please let me know if you have any issue with it! :)