Android send email with attachments using only email apps - java

The official docs show how you can send an email with an attachment:
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);
}
}
It then says:
If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme.
Like so:
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);
}
}
But actually, I want a combination of the above... i.e. send an email with an attachment and using only an email app.
But when using intent.setData(Uri.parse("mailto:")) in combination with Intent.ACTION_SEND or Intent.ACTION_SEND_MULTIPLE, nothing happens... no email app (or app chooser) opens at all.
So how do I send an email with attachment (or multiple attachments) whilst also restricting the app to email apps?

That mailto: URI string appears invalid without recipients; try something alike this instead:
intent.setData(Uri.parse("mailto:" + String.join(",", addresses)));
See RFC 6068: The 'mailto' URI Scheme.

Remove if (intent.resolveActivity(getPackageManager()) != null) check and it will open the intent. Usually the OS returns null whether the email handling apps exist or not.

Related

Add attachment to gmail app via intent in Android

I have tried adding an attachment (csv file) to the mail intent. When the i start the intent and choose Gmail as the app, it says "Permission denied for the attachment". How do i solve this?
This is the code that i'm using
try {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("address", input.getText().toString());
editor.apply();
String gmail=sharedPreferences.getString("address","");
Uri dat = Uri.fromFile(path);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/html");
sendIntent.putExtra(Intent.EXTRA_EMAIL, gmail);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Stuff");
sendIntent.putExtra(Intent.EXTRA_STREAM, dat);
startActivity(Intent.createChooser(sendIntent, "Send email..."));
}catch (Exception e){
System.out.println(e.toString());
}
Your csv file is in private internal storage of your app. No other apps have access. Only your app. So the used email app has no access too. Put the file on another place. Or use a FileProvider.

Intent.EXTRA_STREAM in Android

Why does it say String in Android documentation website, when EXTRA_STREAM is a URI. I'm trying to understand how to read android documentation.
EXTRA_STREAM
String EXTRA_STREAM
A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
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);
}
}
String is the type of the Constant Intent.EXTRA_STREAM, any extra name must be a String.
In information technology, a Uniform Resource Identifier is a string of characters used to identify a resource.

Android Studio, Genymotion - sending email with txt file attachment: display in gmail, but doesn't send

Java, Android Studio, Genymotion.
Dear colleagues,
i'm sending email (Intent) with txt attach from android application. Txt file was created by application earlier.
In genymotion in gmail client this attachment (file around 1 Kb) is displaying, but real mail is coming without attachment.
Code snippets:
// file creating
...
final String FILENAME = "file";
...
try {
// отрываем поток для записи
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(openFileOutput(FILENAME, MODE_PRIVATE)));
// writing any data
bw.write ("\n");
...
Log.d(LOG_TAG, "file is created");
bw.close();
}
// sending email with intent
public void sendEmailwithMailClient (){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
// sending email
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"example#rambler.ru"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.app_name));
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hello!");
File file = new File(getFilesDir(), FILENAME);
// if (!file.exists() || !file.canRead()) {
// return;}
Uri uri = Uri.fromFile(file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));
}
Do i correctly define Uri for attachment via getFilesDir() and FILENAME?
Why email is loosing attachment during sending? It's issue of Genymotion or in reality i'm not attaching anything to mail and attach displaying in Genymotion is just a fake?
Thank you in advance!
You cannot attach a file from the your apps private storage.
You need to save it to external storage and then attach.
File file = new File(getFilesDir(), FILENAME);
is creating your file in /data/data/package_name/files directory.
Which is not accessible form the other apps.
If you still want to share the file from the apps private storage you need to create your ContentProvider.

Passing parameters from Javascript to Android (Java)

I'm trying to send an email through the Android's email client. However, when I attempt to send the E-mail to the client, it says the email is null. I find that strange since I'm passing the correct parameters in JavaScript, but the app wont read it?
I have been researching this issue for awhile now and have searched Stack Overflow for awhile.
Anyways, my goal with this is to understand why my parameters are not being passed through.
JavaScript:
function sendEmailToAndroid (emailAddresses, CC, subject) {
alert(emailAddresses); //This displays the correct address
if (IsAndroidDevice()) {
window.ccs.sendEmail(emailAddresses, CC, subject);
}
}
Java:
#JavascriptInterface
public void sendEmail(String[] emailAddress, String[] CC, String subject){
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, emailAddress);
email.putExtra(Intent.EXTRA_CC, CC);
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.setType("message/rfc822");
if(emailAddress == null || emailAddress.length == 0){
Toast ctoast = Toast.makeText(getApplicationContext(), "There is no email for this customer", Toast.LENGTH_LONG);
ctoast.setGravity(Gravity.CENTER, 0, 0);
ctoast.show();
Log.d(LOG, "Email: "+emailAddress);
}
else{
try{
startActivity(Intent.createChooser(email, "Send mail..."));
} catch(ActivityNotFoundException e){
Toast.makeText(getApplicationContext(), "Failed to initalize email client!", Toast.LENGTH_LONG).show();
}
}
}
Now when I debug the JavaScript, all parameters are being passed correctly. However, when it is read in the android app, all the values come back as null, or undefined. Which is not correct.

How to send email newsletter from android application programmatically

I am trying to send an HTML table through email, but what i only get is a string with the HTML code.
I read about email newsletter a little but i didn't figure out how to make it work in my android test application.
I have a regular table that insert into String string.
String string = "<table border='1' align='center'><tr style='color:blue'><th>Day</th><th>Date</th><th>Start Time</th><th>End Time</th><th>Total Time</th></tr><tr><td align='center'>Sunday</td><td align='center'>19/07/2011</td><td align='center'>13:00</td><td align='center'>19:00</td><td align='center'>06:00</td></tr></table>";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, getSenderList());
intent.putExtra(Intent.EXTRA_SUBJECT, mContext.getText(R.string.app_name));
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(string));
mContext.startActivity(intent);
Someone can please point me how to do that?
Thanks
Check out the following question:
Send HTML mail using Android intent.
If you want to use the default mailer, change your code to the following:
String body = "<table border='1' align='center'><tr style='color:blue'><th>Day</th><th>Date</th><th>Start Time</th><th>End Time</th><th>Total Time</th></tr><tr><td align='center'>Sunday</td><td align='center'>19/07/2011</td><td align='center'>13:00</td><td align='center'>19:00</td><td align='center'>06:00</td></tr></table>";
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + getSenderList()));
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, mContext.getText(R.string.app_name));
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
mContext.startActivity(intent);

Categories

Resources