Android : how to open a mail composer view?
-
20-06-2021 - |
문제
I just want to know how to open Mail Composer in Android.
With iOS, I would do something like this :
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[controller setSubject:@"Mail subject"];
[controller setMessageBody:@"Mail body" isHTML:bool];
[controller setToRecipients:recipientsList];
if(controller) [self presentModalViewController:controller animated:YES];
How about Android ?
Thanks a lot.
해결책
Intent intent=new Intent(Intent.ACTION_SEND);
String[] recipients={"xyz@gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT,"abc");
intent.putExtra(Intent.EXTRA_TEXT,"def");
intent.putExtra(Intent.EXTRA_CC,"ghi");
intent.setType("text/html");
startActivity(Intent.createChooser(intent, "Send mail"));
다른 팁
The list of apps can be limited to email apps only by using ACTION_SENDTO.
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
See https://developer.android.com/guide/components/intents-common.html#Email
If you want to open only the email clients, then:
Intent intent = new Intent(Intent.ACTION_SEND);
String[] recipients = {"wantedEmail@gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, "emailTitle:");
intent.putExtra(Intent.EXTRA_CC, "ghi");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Send mail"));
Mostly similar to the accepted answer, with different MIME type.
Like this:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] emailTo});
emailIntent.putExtra(android.content.Intent.EXTRA_CC, new String[]{emailCC});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailText);
context.startActivity(Intent.createChooser(emailIntent, context.getString("send email using:")));
You can find more details here: http://mobile.tutsplus.com/tutorials/android/android-email-intent/
제휴하지 않습니다 StackOverflow