Friday, April 20, 2012

Sending an Email


The following bit of code enables your app to send an email. If a user has multiple email processing apps, like an email app and Gmail, a picker will automatically be displayed for them to select the email app they want to use to send the message.




 String mMessage="Here is a very important email message that you need to read right away."; 

 String mSubject="Urgent: Read me now!"; 

 String mEmailTo="mom@you.com"; 

 String mCCTo="dad@you.com"; 

 Intent emailI = new Intent(Intent.ACTION_SEND); 

 emailI.setType("text/plain"); 

 if (mSubjectStr!=null) { 

      emailI.putExtra(Intent.EXTRA_SUBJECT, mSubject); 

 } else { 

      emailI.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.emailSubjectVal)); 

 } 

 if (mCCValStr!=null) { 

      emailI.putExtra(android.content.Intent.EXTRA_CC,new String[]{ mCCTo}); 

 } 

 emailI.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { mEmailTo }); 

 emailI.putExtra(android.content.Intent.EXTRA_TEXT, mMessage); 

 startActivity(emailI);