Android : how to open a mail composer view? - android

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/

Related

Find Solution to send email

I am creating utility to send email. In my code i use chooser intent to choose email app to send email. It work perfectly but the problem is that if i use attachment file in this code using Uri then in chooser i choose G-mail, and then G-mail is stopped. If i send email without attachment it work good. Can any one solve my problem. Here is my code.
public void SendEmail() {
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setData(Uri.parse("mailto: "));
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL,to);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
// intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send Mail..."));
}
In this code a line is in comment. if i use this line then G-mail is stopped. I am using this line for file attachment. Help me please.
/**Use the below Code Snippet**/
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("vnd.android.cursor.dir/email");
String to[] = {""};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + "Your URI"));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Use this code it's works for me correctly:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = {"info#marutinandan.com"};
//String aEmailCCList[] = { "user3#fakehost.com","user4#fakehost.com"};
//String aEmailBCCList[] = { "user5#fakehost.com" };
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
// emailIntent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList);
// emailIntent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "your subject");
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "your message body.");
startActivity(emailIntent);

Does the ACTION_SEND Intent have an EXTRA to specify recipient's name?

I am using this Intent to open the email client:
new Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_EMAIL, new String[]{"someLongAndUglyEmailAddress#example.com"})
.putExtra(Intent.EXTRA_SUBJECT, "Subject");
Is there an EXTRA to specify the recipient's actual name? Or is that implemented solely by the email client?
There no extra for that from the doc https://developer.android.com/guide/components/intents-common.html#Email
That's the maximum that you can do to compose a new email
public void composeEmail(String[] addresses, String subject, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}

intent to share text to email clients (only email clients)

String value = text.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test#test.test"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, value);
startActivity(Intent.createChooser(intent, "Send Email"));
this code runs, but it show a list of applications like notepad (and other notepad app), whatsapp (and several chat app).
I need a list of only email clients. I done a long search but the code is always same.
try the following code with content type:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some#email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
Edit1: Check out this post for sending email directly without opening the email client.

Displaying the To address prefilled in Email Intent?

I am not able to pre fill the TO field in Email client to the "to" address mentioned in the extras here:
EmailImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it = new Intent(Intent.ACTION_SEND_MULTIPLE);
it.putExtra(Intent.EXTRA_EMAIL, "toaddress#gmail.com");
it.putExtra(Intent.EXTRA_SUBJECT, "Regarding Policy Info");
it.putExtra(Intent.EXTRA_TEXT, "When is my next Premium due");
//it.setType("text/plain");
it.setType("message/rfc822");
startActivity(it);
}
});
What is the problem?
Thanks
Sneha
You need to put the address in an array:
it.putExtra(Intent.EXTRA_EMAIL, new String[] {"toaddress#gmail.com"});
See here.
I've got something like this and its works:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some#email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
When using ACTION_SEND_MULTIPLE,
You have to provide an array of String for Intent.EXTRA_EMAIL Binyamin Sharet shown you.
If the requirement is to provide only one Address then use Intent.ACTION_SEND.
Try this
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL,new String[]{"","your email"});
This worked for me:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "someone#gmail.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, edt_msg.getText().toString());
emailIntent.putExtra(Intent.EXTRA_SUBJECT, edt_subjct.getText().toString());
emailIntent.setType("message/rfc822");
Uri uri = Uri.parse("file://" + file_img_capt);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(emailIntent);

Email activity on Android

I want to send email from Android virtual machine to my gmail account.
Problem: but on pressing send button I am getting:
"No application can perform this action"
Here is my code:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("audio/mp3");
//sendIntent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
//sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/download/test.mp3"));
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(GlobalVariable.getstrEmail()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(sendIntent, "Title:"));
Try it on a real device itself, it should work. And you need to change the type:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"email#example.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "body text");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
This can help...
Intent openEmailIntent = new Intent(android.content.Intent.ACTION_SEND);
openEmailIntent.setType("plain/text");
openEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[{"zoombie#gmail.com"});
openEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"subject you want");
openEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Any body ");
this.startActivity(Intent.createChooser(openEmailIntent,"Sharing via Email"));
try this....
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailaddress});
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Categories

Resources