Saturday, April 14, 2012

How to Send Email without User Interaction (In Background) in Android ?


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.
http://code.google.com/p/javamail-android/
http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android
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.
Code Snippet To Send Email
       Mail m = new Mail("xyz@example.com", "<PASSWORD>");
        String[] toArr = {"abc@example.com"};
        m.setTo(toArr);
        m.setFrom(" xyz@example.com ");
        m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
        m.setBody("Email body");
        try {
          m.addAttachment("/sdcard/bday.jpg");
          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);
        } 

No comments:

Post a Comment