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

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

Related

How do i get information typed on editText?

So I have an editText in an app I created, the aim of this editText is to send a piece of text information. I want the user to be able to send text information for me to use, it's kinda like when you have a "FeedBack" option and the user has to fill it with the required information needed, I want to be able to get this information somehow. Pls help, I'm still new to the mobile app development thing. :)
Use getText() on your EditText. This will return an Editable which can be casted to String if you want:
String feedback = editTextFeedback.getText().toString();
If you want to receive the input by mail:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.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(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
What you can do is opening a "share" activity and send the text through any app you have in the phone (whatsapp, telegram, mail, etc).
public void share(message: String) {
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Sending message");
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
Intent.createChooser(shareIntent, "Share with");
} catch (Exception e) {
e.printStackTrace()
}
}
This will open a window like this:

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.

Android: Email 'to ' field empty

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!

Sending email programatically using the DEFAULT app

I am developing an app that send programatically an email, Im using stoxk LG mail
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{XXXXXXX});
i.putExtra(Intent.EXTRA_SUBJECT, "XXXXXXXXXX");
i.putExtra(Intent.EXTRA_TEXT , (" XXXXXXXX"));
try {
startActivity(Intent.createChooser(i, "Sending mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast toast = Toast.makeText(getApplicationContext(), "Not found ", Toast.LENGTH_LONG);
toast.show();
finish();
}
It opens the LG mail and pastes the SUBJECT, the TEXT and the MAIL BUT it doesn't send it , how can I achieve it?
Anny suggestion will be apreciated!
With intent you can't do this.
But look at the link, there is an answer for your question.
Sending Email in Android using JavaMail API without using the default android app(Builtin Email application)

Android: Separate intents for email & SMS

I have an app that has two buttons – one for email & one for SMS. Depending on the button pressed I want to email or else SMS a certain text. I have coded the email button and it is working fine. The problem is that the dialog box that pops-up gives an option of e-mail or Messaging the text. I want to separate out the two, so that when the user presses email, only the options of email is there, and when the user presses SMS, only the option of Messaging is there.
Here is the code that I have tried.
private void sendEmail(){
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject of the message");
i.putExtra(Intent.EXTRA_TEXT , "Body of the message");
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();
}
}
Basically there seems to be a single intent Intent.ACTION_SEND for both emails & Messaging.
Any way to separate them out?
You can launch the Messaging application with a prepopulated message like this:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "The SMS text");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
or do something like this to just send it straightaway, without presenting the Messaging app:
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
String to use for sending email only is:
email.setType("message/rfc822");

Categories

Resources