How to send email newsletter from android application programmatically - java

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);

Related

Android send email with attachments using only email apps

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.

How to make a reusable java function for android webview

Please am very new to java, am trying to put my website in webview, i have few lines of code that send user to email form and is working very fine. But now i have to use the same code in more than 3 place when url match something and i know that there will be a way to put this code one place as a function then call it and time i want to use it. please can someone help me.
function RequestMailForm(newbody, newsubject, newemailto, newmailbbc){
/* That email code*/
}
mva.setWebViewClient(new WebViewClient() {
if (url.startsWith("mailto:")) {
url = url.substring(7);
String body = "Body of message.";
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("application/octet-stream");
mail.putExtra(Intent.EXTRA_EMAIL, new String[] { url });
mail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
mail.putExtra(Intent.EXTRA_TEXT, body);
startActivity(mail);
return true;
}
if (url.startsWith("http://example.com/help") || url.startsWith("https://example.com/contact")){
RequestMailForm(newbody, newsubject, newemailto, newmailbbc);
}
});
Create a class and put sendEmail method with message and email paramater.
See the code below.
class SendEmail {
Context context;
public SendEmail(Context context){
this.context = contex;
}
//send an email
public void send(String email) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", email, null));
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body of message");
context.startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
}
Then create an object of the SendEmail class and call that function every time you want to send an email like this:
if (url.startsWith("mailto:")) {
url = url.substring(7);
sendEmail.send(url);
return true;
}

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.

Categories

Resources