I looked it up, and this is the most common way to send an email...
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();
}
I am confused as to what this is actually sending, and how to make this happen when the user clicks a button. Do I put the recipient email (me) like this?
i.putExtra(Intent.EXTRA_EMAIL , "myemail#gmail.com");
Then the same format for subject and message/body of the email?
Is this how I would put the user's input into the body of the email? (The user inputs multiple editText boxes)
editText userTitle = (editText)findViewById(R.id.idOfTheEditTextBox);
editText userDescription = (editText)findViewById(R.id.idOfTheEditTextBox);
Then input it like this?
i.putExtra(Intent.EXTRA_TEXT , "userTitle", "userDescription");
Finally, what does all that toast and no email clients installed mean? I am new to android app development, and am making an app on Android Studio! All help is very much appreciated!! Thanks!
Firstly "Toast"
Toast code is written to give alert to user, notifying that no email
client is available in users phone (ex. gmail, etc)
Now the main portion,
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
Yes this above line add the recipents email address.
So to achive this kind of thing, you need to do following things,
Create layout with one Edittext (where user can put recepeints email address which you can pass to Intent). And one button on click of which you will launch Intent.
Now then the button is clicked write this code:-
public class MainActivity extends AppCompatActivity {
EditText etRecipentId, etSubject, etBody;
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etRecipentId = (EditText) findViewById(R.id.email_id);
etSubject = (EditText) findViewById(R.id.et_subject);
etBody = (EditText) findViewById(R.id.et_body);
}
b1.setOnClickListener(new OnClickListener() {
public void onClick() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[] {
etRecipentId.getText().toString();
});
i.putExtra(Intent.EXTRA_SUBJECT, etSubject.getText().toString(););
i.putExtra(Intent.EXTRA_TEXT, etBody.getText().toString(););
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();
}
}
});
instead of i.putExtra(Intent.EXTRA_TEXT , "userTitle", "userDescription"); use i.putExtra(Intent.EXTRA_TEXT , userTitle+" "+userDescription);
And as you are getting exception ,check that is there any email client like gmail,etc is installed in your phone or not.
Related
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:
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 , sb.toString());
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();
}
The sb.toString() is a stringbuilder that holds the appropriate data.
At first, I thought everything working fine because I could open the email app and the appropriate text was displayed. However, after pressing send I found that when I looked at the mail in my gmail client (on phone and on computer) it was gibberish. What is causing this and how do I fix it?
I am using gmail mail client.
Here is what I see in the email after I send it to myself:
� 407,497,552
repeated many, many times.
Before I press send, I see: integer, integer, integer
on each line for a 50-75 lines.
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)
Hi I am using in built email sending functionality.But when I tried the code.
void sendEmailMessage(String emailId)
{
Log.i(TAG, "emailId = "+emailId);
Intent intentEmail = new Intent(Intent.ACTION_SEND);
intentEmail.setType("text/plain");
String[] recipients = new String[]{emailId};
intentEmail.putExtra(Intent.EXTRA_EMAIL,recipients);
intentEmail.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
intentEmail.putExtra(Intent.EXTRA_TEXT, "body of email");
try
{
startActivity(Intent.createChooser(intentEmail, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
}
I don't see recipient email address in recipient EditText on Email screen.I am not getting what I am doing wrong please help.
I think in your void sendEmailMessage(String emailId) method's emailId argument didn't have the value. Just check where you've been called from. And, make sure you are passing email id or not? For example. In your class's somewhere just call that method like below with value.
sendEmailMessage("mail#mail.com");
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");