[ Android Email Intent and StartActivityForResult - Send Two Emails Sequentially? ]
I have a simple routine I want to implement but I am not sure if I am going about it the right way. I want to use an emailintent to send a first email then following that to call another emailintent and send a second email (which will be different from the first). In essence I want to sequentially send two emails.
I tried startActivityforResult for the first email and then calling the second emailintent on that request code but this doesn't seem to work (I know Android won't give me a result code if I use StartActivityforResult to send emails externally though that doesn't concern me as the second emailintent should be called regardless of whether first was successful).
Any ideas?
Answer 1
Your approach is right way to do it. Here is some sample code which worked on a device.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1234) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("message/rfc822");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "address2@example.com" });
// Setup message here
try {
getSupportActivity().startActivity(sendIntent);
} catch (final android.content.ActivityNotFoundException ex) {
// No email client found
}
}
}
public void sendEmail() {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("message/rfc822");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "address@example.com" });
// Setup message here
try {
getSupportActivity().startActivityForResult(sendIntent, 1234);
} catch (final android.content.ActivityNotFoundException ex) {
// No email client found
}
}
If you are using fragments, make sure the onActivityResult is in the Activity or make sure to redirect the onActivityResult from the activity to the fragment.
Answer 2
You can use the Javamail API to send two different emails. I guess this sould work with javamail. http://docs.oracle.com/cd/E18930_01/html/821-2418/beaow.html#scrolltoc
(Don't forget to import libraries and add internet permission)