How can I make email sent from android java? - android

I have an POS app and I want to send my e-receipt to customers via email.
I have checked some resources and what I have found is how to send emails with text messages.
How can I make my email to look something like this?

You can pass Spanned text in your extra
final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "The Subject");
shareIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<p><b>Some Content</b></p>")
.append("<small><p>More content</p></small>")
.toString())
);

I think you can send email via Intent action
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, new String[]{"support#tutlane.com"});
it.putExtra(Intent.EXTRA_SUBJECT, "Welcome to Tutlane");
it.putExtra(Intent.EXTRA_TEXT, "Hi Guest, Welcome to Tutlane Tutorial Site");
it.setType("message/rfc822");
https://www.tutlane.com/tutorial/android/android-send-email-with-examples

You can try the following way.
String[] recipients = {"abc#gmail.com"};
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
// prompts email clients only
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, recipients);
email.putExtra(Intent.EXTRA_SUBJECT, "email subject");
email.putExtra(Intent.EXTRA_TEXT, "Hello, This is a test message");
try {
// the user can choose the email client
startActivity(Intent.createChooser(email, "Choose an email client from..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "No email client installed.",
Toast.LENGTH_LONG).show();
}
}
Android provides some further fields, these fields have to be attached to the Intent as extra data:
EXTRA_BCC: email addresses for blind carbon copy
EXTRA_CC: email addresses for carbon copy
EXTRA_HTML_TEXT: supply an alternative to EXTRA_TEXT as HTML formatted text
EXTRA_STREAM: URI holding a stream of data supplying the data that
are sent
EXTRA_TITLE: the title that is shown when the user has to choose an
email client
For a complete example, see this tutorial.

Related

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!

Android not prompting me apps with email Capability

I'm trying to make a app, that takes information of some sort, then i want it to email that information to my gmail. I have found working code but when i load it onto my phone and run it and got all the info loaded into the app,and click the email, from what i understand its suppose to filter apps(on my phone) that are capable to send the email but I'm not getting anything, even though i have the default Email app that comes on the phone and i have Gmail.
public void Done(View view) {
Intent email = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
email.putExtra(Intent.EXTRA_EMAIL, "some#gmail.com");
email.putExtra(Intent.EXTRA_SUBJECT, "OverStock Changes");
email.putExtra(Intent.EXTRA_TEXT, printReport());
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Email"));
}
See answer for ACTION_SENDTO for sending an email
If you use ACTION_SENDTO, putExtra() does not work to add subject and
text to the intent. Use setData() and the Uri tool add subject and
text.
This example works for me:
// ACTION_SENDTO filters for email apps (discard bluetooth and others)
String uriText =
"mailto:youremail#gmail.com" +
"?subject=" + URLEncoder.encode("some subject text here") +
"&body=" + URLEncoder.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email"));
Otherwise use ACTION_SEND as mentioned:
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"mail#mail.com","mail2#mail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT,"subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail content");
startActivity(Intent.createChooser(intent, "title of dialog"));

how to send mail to an email id in android?

In my project i have to send mail to company id.First i tried code with my email id. but its not showing anything.my code is here
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = { "my emailid used here" };
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject");
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "My message body.");
startActivity(emailIntent);
Log.e("name","msg sent success fully");
in my logcat i saw "msg sent success fully" message also. but i didnt get any mail in my mail id.
am i did any wrong code or am i need to add any permissions in manifest file.
Your code seems fine except there is no need to use "android.content." and there is no FLAG_ACTIVITY_NEW_TASK What should happen is that your email program should start with these data already entered. Have you setup your email client on that device?
Here is a code example that works and sends an image selected from a gallery as an attachment:
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("image/png");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + mUrls[imageSelected].toString()));
startActivity(i);

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