Aug 12, 2011

Send Email without User Interaction (In Background)

Android provide Intent to send the email. It compose email in Native Email editor and user need to click on Send button to send the email. It crease issue when application need to send more then one email. User will not click on send button 10 or 20 times.

To solve this issue. I found javaMail API implemented for Andorid.

I have create Demo project, attached with this article. JavaMail also allowed to send email with attachment and HTML as body content. JavaMail required email user name & password to send the email.

You can find "Mail" class in attached source code.  In following code you need to replace USER EMAIL, PASSWORD, user name and password is require to send email.

Code Snippet To Send Email
        Mail m = new Mail("USER EMAIL", "PASSWORD"); 
        String[] toArr = {"EMAIL-1", "EMAIL-2"}; 
        m.setFrom("USER EMAIL"); 
        m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device."); 
        m.setBody("Email body"); 
        try { 
          if(m.send()) { 
            Toast.makeText(this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
          } else { 
            Toast.makeText(this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
        } catch(Exception e) { 
          Log.e("MailApp", "Could not send email", e); 

Click Here to Download the Source Code
 Note: To download source code click on File->Download Original File