Send a mail through an android app - android

I've implemented a class that can send a mail. However, you can only write a topic and the message and when you click on "Send" you've to choose which app (Outlook or Gmail) you want to send the mail with and then write your e-mail. However, I want to make it possible for the user to send a mail directly from the my app instead of using another app. So I want to make it possible for the user to write his/her e-mail/gmail and message and then send the message to my e-mail. So in other words in the fragment I want an EditText where the user writes his/her e-mail/gmail, another EditText where the user writes the message and a button to send. How can this be implemented?
This is what I have done in my app to send a mail:
private void sendemail(String message) {
String [] reciever = new String[]{"myemail#hotmail.com"};
String subject = ("Feedback/Question");
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.putExtra(Intent.EXTRA_EMAIL, reciever);
mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
mailIntent.putExtra(Intent.EXTRA_TEXT, message);
mailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(mailIntent, "Choose an application to send your mail with"));
}

Use this library -> Send email in background. It will send an email from your app without users interaction.
Cheers!

using java mailApi,you must have to authenticate gmail app using credential.
please check this link:
Email via Java MailAPI
other link to provide custom class to send mail programatically.
https://stackoverflow.com/a/4345084/1223291
I hope its useful to you.

Use this method....
public void sendEmail()
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"care#xyz.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "care Feedback");
intent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(intent, "Send Email"));
}

Related

How can I avoid saving draft of gmail intent?

Part of my application functionality is to automatically fill gmail message subject, text, recipients and attach a photo. But I don't want to save this message as draft when I exit gmail intent without sending out the message.
How can I disable saving a draft before starting intent from my application? Is there a possible solution or workaround?
My intent code:
// fill email message fields for sending out report
public void composeEmail(String[] addresses, String subject) {
File filelocation = new File(mCurrentPhotoPath);
Uri photoUri = Uri.fromFile(filelocation);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, photoUri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Part of my application functionality is to automatically fill gmail message subject, text, recipients and attach a photo.
Your code has nothing specific to do with Gmail. There are many email clients for Android, and not everybody uses Gmail.
How can I disable saving a draft before starting intent from my application?
Write your own email client, rather than delegating to an existing email client. Otherwise, the decision of what goes on inside that email client is up to the user and the developers of that email client, not you.

How can I create a simple program that it can send a text to a gmail address?

How can I create a simple program that it can send a text to a gmail address?
I want to create a simple program that it have a EditText and when I write some text in the EditText and I click on the Button, as the result the text be sent to a gmail address, for example to the example#gmail.com. I know how to create a EditText class but I do not know the operation post to the gmail. Thank you.
In Android, you can use Intent.ACTION_SEND to call an existing email client to send an Email.
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"put_your_mail#gmail.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
For more details you visit How to send Email in Android and
http://code.tutsplus.com/tutorials/quick-tip-enabling-users-to-send-email-from-your-android-applications-the-easy-way--mobile-1686
Standard way to do this is using the ACTION_SEND intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, "emailaddress#emailaddress.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
startActivity(Intent.createChooser(intent, "Send Email"));
If you want to send the mail in background without opening another app check this answer https://stackoverflow.com/a/2033124/2761055
Here, you have information how to do that in background with PHP file on your server.
But in my opinion, this method is deprecated.
Look also here. This code will start new intent with email.

Android intent not showing only email clients

So I'm trying to launch a prepopulated email client with data. The content gets populated fine, however my problem is that when launching the intent, I wanted it to only show email clients to select from.
Instead, it shows Gmail, Adding to EverNote, Android Beam, Bluetooth, and some others.
I don't know if its an issue with lollipop that broke this functionality or not, as one of my managers sent me code that worked fine for him a few years ago.
My code is:
private void openEmailClient(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{getResources().getString(R.string.contact_feedback_email_address)});
intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.contact_feedback_email_subject_android));
try{
startActivity(Intent.createChooser(intent,intentEmailString));
} catch(android.content.ActivityNotFoundException ex){
Log.e(EMAIL_FAIL_TAG, EMAIL_FAIL);
ex.printStackTrace();
}
}
when you will change your intent.setType like below you will get
intent.setType("text/plain");
Use
android.content.Intent.ACTION_SENDTO
(new Intent(Intent.ACTION_SENDTO);) to get only the list of e-mail clients, with no facebook or other apps. Just the email clients.
I wouldn't suggest you get directly to the email app. Let the user choose his favorite email app. Don't constrain him.
If you use ACTION_SENDTO, putExtra does not work to add subject and text to the intent. Use Uri to add the subject and body text.
We can use message/rfc822 instead of "text/plain" as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.
message/rfc822 supports MIME Types of .mhtml, .mht, .mime
EDIT
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:someone#example.com"));
intent.putExtra("subject", "my subject");
intent.putExtra("body", "my message");
startActivity(intent);
its working ...
So I solved it. Not ideally but it works better than anything else I have tried.
I followed the google docs on doing it, which says to do this:
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);
}
}
and it now works.
This just finds a default app for handling mail. I'm not sure how it decides, but in my case it opened GMail. On a device without GMail installed, such as the Galaxy S5, it opened their mail client and prompted the user to set up email.
Doesn't give choice of app but it works
Try like this it working fine for me...
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:abc#gmail.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Test App");
intent.putExtra(Intent.EXTRA_TEXT, "Email Body");
startActivity(intent);
Note: it only work if you have email address.
For more information please refer this link Android - Is there a foolproof way to only show possible EMAIL clients?

android fragment check send mail finish(android show dialog when mail send success)

I have fragment. when click button,it send mail, but i don't know when mail send success, after i show dialog success.
my code send :
//Intent action send
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL,
new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT,
"bbbbb");
email.putExtra(Intent.EXTRA_TEXT,
"AAAAAAA"));
//need this to prompts email client only
email.setType("message/rfc822");
getActivity().startActivity(Intent.createChooser(email,
"Send mail..."));
i want show dialog when mail send success.
Please help me. thanks
It seems like there is no good solution for this.
The 'normal' way would be to use startActivityForResult() as you send your intent, but Intent.ACTION_SEND gives no output (documentation) so this doesn't work either. See this question.
The only way to do it for sure would be to make your own activity to send the email, and return a result from that.

How to send mail from Gmail in android?

I am building an android application where an user is required to lo-gin using g+ to enter in the app. After lo-gin there is 3 EditTextView where user enter required data respect to their field to send mail. the field like - recipient mail Id, subject, and body.
Now the problem is How to send the mail to that recipient ID. using my app.
Here you go:
https://stackoverflow.com/a/2197841/3498044
https://stackoverflow.com/a/6264746/3498044
Next time search before you ask
Try with Following
private String[] recipients = {"abc#gmail.com"};
private String type = "text/html";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "text");
intent.setType(type);
startActivity(Intent.createChooser(intent, "Send mail"));

Categories

Resources