Android: Email 'to ' field empty - android

I am developing an application for a hotel, where in, on booking a room, the app sends an email to the email-id given by the user. Now, i know it will use one of my default email clients, which is the gmail. The problem is, it is showing up with gmail's compose message window, with my message in the message body,but the 'to'field is empty. Any help?
here is the code:
public void sendmail(View vw)
{
name=et1.getText().toString();
to=et2.getText().toString();
phone=et3.getText().toString();
addr=et4.getText().toString();
Log.i("Send email", "");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Thanks for using our app");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hello mr"+name+"We just received an email with your details asking for a reservation:"+phone+" "+addr+"for room number"+x+"");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getBaseContext(), "There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
}

From the documentation of EXTRA_EMAIL:
A String[] holding e-mail addresses that should be delivered to.
Something like this should work:
Intent mailIntent = new Intent(Intent.ACTION_SENDTO);
mailIntent.setData(Uri.parse("mailto:"));
mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Thanks for using our app");
mailIntent.putExtra(Intent.EXTRA_TEXT, "Hello mr"+name+"We just received an email with your details asking for a reservation:"+phone+" "+addr+"for room number"+x+"");
if (mailIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mailIntent);
} else {
// no e-mail app installed
}

Try this :
emailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#to.com"});
email intent expects an String array but you're providing a string.So, it's not working in your case!

Related

How to Send Emails?

I found an algorithm to send an email through an Android app. When I run it, it opens a list of apps I can choose to send it, however, I only wanted to use the Gmail app.
This is the code:
protected void sendEmail(String subject, String text) {
Log.i("Send email", "");
String[] TO = {"email#gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, text);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email!", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
Thanks in advance!
Implicit Intents declare a general action to perform, in your case every app that has an intent filter of an e-mail client, this can be Gmail, Outlook or whatever e-mail client that your device has.
In this case you can try this code:
Intent sendIntentGmail = new Intent(Intent.ACTION_VIEW);
sendIntentGmail.setType("plain/text");
sendIntentGmail.setData(Uri.parse(TextUtils.join(",", addresses)));
sendIntentGmail.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntentGmail.putExtra(Intent.EXTRA_EMAIL, addresses);
if (subject != null) sendIntentGmail.putExtra(Intent.EXTRA_SUBJECT, subject);
if (body != null) sendIntentGmail.putExtra(Intent.EXTRA_TEXT, body);
mContext.startActivity(sendIntentGmail);
You can use below code:
Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm",
"com.google.android.gm.ConversationListActivity");
startActivity(mailClient);

Send email from Android App without user's intervention

I would like to send an email to given email address with some default instructions. I know that I can achieve it for sms by using twilio but don't know how to do this via mail..
You can directly use following to send mail with body:
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String\[\]{ "serveroverloadofficial#gmail.com"});
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hello There");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Add Message here");
intent.setType("message/rfc822");
try {
startActivity(Intent.createChooser(intent,
"Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(),
"No email clients installed.",
Toast.LENGTH_SHORT).show();
}
}
});
Use Mailgun.com to send email by Using their API.

How to send an email in Android without action to UI?

I want to use Intent to send an email without any touching in the screen. I tried the bellow code but it requires to choose many steps: such as email client, send button...I do not want to perform these steps. Just auto send without any touching in the screen. Is it possible in Android M? Thanks
//Email
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"abc#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Title");
i.putExtra(Intent.EXTRA_TEXT , "Body");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
using Intent with ACTION_SEND should open all Apps that can handle sending emails and user will have to select one of them to proceed with sending.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:user_name#provider"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT,"Body");
try {
startActivity(Intent.createChooser(intent, "Send Email"));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
but if want to send email without using other apps (email clients), you should search for how to send emails directly or check this

How to skip the chooser dialog for sending mail in android

I'm getting the pop-up for choosing the email client (Gmail, EMail, etc.), but I want to skip it and instead send it directly through GMail.
protected void sendEmail() {
String[] recipients = { recieverId.getText().toString() };
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, recipients);
email.putExtra(Intent.EXTRA_SUBJECT, mailSubject.getText().toString());
email.putExtra(Intent.EXTRA_TEXT, mailBody.getText().toString());
try {
startActivity(Intent.createChooser(email,
"Choose an email client from..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(),
"No email client installed.", Toast.LENGTH_LONG).show();
}
}
Simply omit createChooser and hardcode Gmail intent, as described here:
public void sendGmail(Activity activity, String subject, String text) {
Intent gmailIntent = new Intent();
gmailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
gmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
gmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
try {
activity.startActivity(gmailIntent);
} catch(ActivityNotFoundException ex) {
// handle error
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent emailIntent = new Intent();
emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");// Package Name, Class Name
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, "Your Sender Mail ID");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body of the Email ");
startActivity(emailIntent);
}
You can get the Package name via adb shell Command:
pm list packages -f
emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");// Package Name, Class Name
Setting class name in intent worked for me. No need to add chooser. For sending email through your office mailclient you can add From (for intent) or select from the list of mail ids from gmail account compose section (From).

Using Android Intent.ACTION_SEND for sending email

I'm using Intent.ACTION_SEND to send an email. However, when I call the intent it is showing choices to send a message, send an email, and also to send via bluetooth. I want it to only show choices to send an email. How can I do this?
[Solution for API LEVEL >=15]
I've finally succeded in sending email WITH attachments to ONLY email clients.
I write it here because it took me a lot of time and it may be usefull to others.
The problem is:
Intent.ACTION_SENDTO takes Data URI (so you can specify "mailto:"
schema) BUT it does not accept Intent:EXTRA_STREAM.
Intent.ACTION_SEND accepts Intent:EXTRA_STREAM (so you can add
attachment) BUT it takes only Type (not Data URI so you cannot
specify "mailto:" schema).
So Intent.ACTION_SEND lets the user choose from several Activities, even if you setType("message/rfc822"), because that App/Activities can manage all file types (tipically GDrive/Dropbox Apps) and so even email message files.
The solution is in the setSelector method.
With this method you can use Intent.ACTION_SENDTO to select the Activity, but then send the Intent.ACTION_SEND Intent.
Here my solution code (the attachment came from a FileProvider, but it could be any file):
{
Intent emailSelectorIntent = new Intent(Intent.ACTION_SENDTO);
emailSelectorIntent.setData(Uri.parse("mailto:"));
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"address#mail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
emailIntent.setSelector( emailSelectorIntent );
Uri attachment = FileProvider.getUriForFile(this, "my_fileprovider", myFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, attachment);
if( emailIntent.resolveActivity(getPackageManager()) != null )
startActivity(emailIntent);
}
I'm not taking credit for this answer but I believe it gives the best answer for this post.
It's a common misconception to use text/plain or text/html. This will trigger any application that can handle plain or HTML text files without any context, including Google Drive, Dropbox, Evernote and Skype.
Instead use a ACTION_SENDTO, providing the mailto: Uri
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
You can then proceed using the chooser as suggested through the other answers.
Answered by #PaulLammertsma here
Android email chooser
Intent email = new Intent(android.content.Intent.ACTION_SEND);
email.setType("application/octet-stream");
EDIT:
You could try setting the type to "message/rfc822" as well.
try this...
#Ganapathy:try this code for display gmail
Intent gmail = new Intent(Intent.ACTION_VIEW);
gmail.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
gmail.putExtra(Intent.EXTRA_EMAIL, new String[] { "jckdsilva#gmail.com" });
gmail.setData(Uri.parse("jckdsilva#gmail.com"));
gmail.putExtra(Intent.EXTRA_SUBJECT, "enter something");
gmail.setType("plain/text");
gmail.putExtra(Intent.EXTRA_TEXT, "hi android jack!");
startActivity(gmail);
This will help you.
On your button click :
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail#yahoo.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
Using message/rfc822 type as pointed here: ACTION_SEND force sending with email solves the issue.
I had a similar problem with my app. I recently found this link form the official android developers site that really helps!
Common Intents: Email
TL;DR:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
Now, you will only be shown email clients!
You can add a Subject and Body by doing this:
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body" );
I notice, that this is an pretty old question but it is the first result when searching for a "Send mail" solution and all answers have a common problem:
Using Intent.ACTION_SEND and intent.setType("message/rfc822") will result in a chooser that not only shows mail apps but all apps that can handle any MIME type support by message/rfc822, e.g. .mhtml, .mht, .mime. Beside mail apps this could be Google Drive, Dropbox, Evernote, etc.
The only solution I found to limit the chooser to mail apps only is to use Intent.ACTION_SENDTO instead:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","address#example.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "My Mail");
intent.putExtra(Intent.EXTRA_TEXT , "My Message");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Thanks to the Open source developer, cketti for sharing this concise and neat solution. It's the only method that worked for me.
String mailto = "mailto:bob#example.org" +
"?cc=" + "alice#example.com" +
"&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(bodyText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
try {
startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
//TODO: Handle case where no email app is available
}
And this is the link to his/her gist.
First solution: try to be more specific in your Intent parameters. Add a message recipient for instance
emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"user#example.com"});
Second solution: use the package manager to find all applications capable of sending a message and select the only those you want to use.
Shout-out to ARLabs for posting the best solution on how to send an email on android. I just made a little update - an option to add multiple email attachements.
fun sendEmail(context: Context, recipients: List<String>?, subject: String?, body: String?, attachments: List<Uri>?) {
val emailIntent = Intent(Intent.ACTION_SEND_MULTIPLE)
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
emailIntent.selector = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
recipients?.let {
val recipientsArray = arrayOfNulls<String>(recipients.size)
ArrayList(recipients).toArray(recipientsArray)
emailIntent.putExtra(Intent.EXTRA_EMAIL, recipientsArray)
}
subject?.let {
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
}
body?.let {
emailIntent.putExtra(Intent.EXTRA_TEXT, body)
}
if (attachments?.isNotEmpty() == true) {
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(attachments))
}
try {
context.startActivity(emailIntent)
} catch (e: ActivityNotFoundException) {
// TODO handle "no app to handle action" error
}
}
This is a combination of Jack Dsilva and Jignesh Mayani solutions:
try
{
Intent gmailIntent = new Intent(Intent.ACTION_SEND);
gmailIntent.setType("text/html");
final PackageManager pm = _activity.getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(gmailIntent, 0);
String gmailActivityClass = null;
for (final ResolveInfo info : matches)
{
if (info.activityInfo.packageName.equals("com.google.android.gm"))
{
gmailActivityClass = info.activityInfo.name;
if (gmailActivityClass != null && !gmailActivityClass.isEmpty())
{
break;
}
}
}
gmailIntent.setClassName("com.google.android.gm", gmailActivityClass);
gmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "yourmail#gmail.com" });
gmailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
gmailIntent.putExtra(Intent.EXTRA_CC, "cc#gmail.com"); // if necessary
gmailIntent.putExtra(Intent.EXTRA_TEXT, "Email message");
gmailIntent.setData(Uri.parse("yourmail#gmail.com"));
this._activity.startActivity(gmailIntent);
}
catch (Exception e)
{
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "yourmail#gmail.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_CC, "cc#gmail.com"); // if necessary
i.putExtra(Intent.EXTRA_TEXT, "Email message");
i.setType("plain/text");
this._activity.startActivity(i);
}
So, at first it will try to open gmail app and in case a user doesn't have it then the second approach will be implemented.
Best code to restrict it to only send an email. set this type to only send an email. i.setType("message/rfc822");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"skapis1#outlook.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Firstclass.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
This saved my day. It sends composed text message directly to gmail app:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","togmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report message");
emailIntent.putExtra(Intent.EXTRA_TEXT, edt_msg.getText().toString());
startActivity(Intent.createChooser(emailIntent, "Send email..."));
This will open only the Email app installed in your android phone.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"example#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "email subject");
intent.putExtra(Intent.EXTRA_TEXT, "message body");
try {
startActivity(Intent.createChooser(intent, "send mail"));
} catch (ActivityNotFoundException ex) {
Toast.makeText(this, "No mail app found!!!", Toast.LENGTH_SHORT);
} catch (Exception ex) {
Toast.makeText(this, "Unexpected Error!!!", Toast.LENGTH_SHORT);
}
public class MainActivity extends AppCompatActivity {
private EditText edt_email;
private Button btn_send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_email = (EditText) findViewById(R.id.edt_email);
btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SEND );
intent.putExtra(Intent.EXTRA_EMAIL , new String[]{"sanaebadi97#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT , "subject");
intent.putExtra(Intent.EXTRA_TEXT , "My Email Content");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent , "Choose Your Account : "));
}
});
}
}
try with ACTION_VIEW not ACTION_SENDTO , ACTION_VIEW will makes system calls only to Email Apps
Intent emailIntent = new Intent(Intent.ACTION_VIEW);

Categories

Resources