Send a mail in android studio - java

I have this code which is working completely error free, but not as per the expected output. When it runs it should provide a choice on how to send a mail, but it provides me with only Bluetooth and messenger not email.
protected void sendEmail() {
Toast.makeText(MainActivity.this,"Sending mail", Toast.LENGTH_SHORT).show();
String[] TO = {"xyz#gmail.com"};
String[] CC = {"abc#gmail.com"};
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_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message :");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}

use this
protected void sendEmail() {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:aaaaaa#gmail.com"));
emailIntent.putExtra("subject", "Feedback");
emailIntent.putExtra("body", "");
startActivity(emailIntent);
}

Related

I cant run the email intent code on my phone

enter image description here
the email intent code is completely run in the android mobile model provided by android studio but when I run the code in my personal phone its not work…
please answer ??
Don't forget to set the type of intent so it will trigger email clients
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
//need this to prompts email client only
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
Kotlin code,
val selectorIntent = Intent(Intent.ACTION_SENDTO)
selectorIntent.data = Uri.parse("mailto:")
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf<String>("mail id"))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject")
emailIntent.selector = selectorIntent
startActivity(Intent.createChooser(emailIntent, "Send email..."))
Java code,
Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"mail id"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.setSelector(selectorIntent);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Intent intEmail = new Intent(Intent.ACTION_SENDTO);
intEmail.setType("plain/text");
intEmail.setData(Uri.parse("mailto:"));
intEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{"receiver_email_address"});
if (intEmail.resolveActivity(getPackageManager()) != null){
startActivity(intEmail);
}
I tested the code of mayar hassan, it worked fine on my side. [on Samsung Android 8.1]
The code:
public class TestSentMail extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_sent_mail);
Button button = findViewById(R.id.submitButton);
button.setOnClickListener(view -> {
submitOrder();
});
}
private void submitOrder() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "coffe order for tancolo");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
The screenshot:
How to fix your problem
In general, I think we should do these as below.
Check the log in the Logcat panel in Android Studio, maybe there are exceptions.
Added your debug code where you want to add.
val i = Intent(Intent.ACTION_SEND)
i.type = "message/rfc822"
i.putExtra(Intent.EXTRA_EMAIL, arrayOf<String>("sumsolutions.net#gmail.com"))
i.putExtra(Intent.EXTRA_SUBJECT, "Feedback")
i.putExtra(Intent.EXTRA_TEXT, "Text here...")
try {
startActivity(Intent.createChooser(i, "Send mail..."))
} catch (ex: ActivityNotFoundException) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT)
.show()
}

How to open gmail in android

I just wanted to open the Gmail app through my app and wanted to set email, subject and message from my application.
I have tried GmailService but it is not supporting bcc or cc emails.
Link: https://github.com/yesidlazaro/GmailBackground
BackgroundMail.newBuilder(this)
.withUsername("username#gmail.com")
.withPassword("password12345")
.withMailto("toemail#gmail.com")
.withType(BackgroundMail.TYPE_PLAIN)
.withSubject("this is the subject")
.withBody("this is the body")
.withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
#Override
public void onSuccess() {
//do some magic
}
}).withOnFailCallback(new BackgroundMail.OnFailCallback() {
#Override
public void onFail() {
//do some magic
}
}).send();
I would like to use bcc and cc functionality along with the attachment, subject, and message.
// For Email by Any app
Intent email= new Intent(Intent.ACTION_SENDTO);
email.setData(Uri.parse("mailto:your.email#gmail.com"));
email.putExtra(Intent.EXTRA_SUBJECT, "Subject");
email.putExtra(Intent.EXTRA_TEXT, "My Email message");
startActivity(email);
open gmail via Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("abc#gmail.com"));
intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
intent.putExtra(Intent.EXTRA_CC, new String[]{"xyz#gmail.com"});
intent.putExtra(Intent.EXTRA_BCC, new String[]{"pqr#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "your subject goes here...");
intent.putExtra(Intent.EXTRA_TEXT, "Your message content goes here...");
startActivity(intent);
just pass EXTRA_CC & EXTRA_BCC in intent argument
Edit
Below answer will work on android 11
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Your subject here...");
intent.putExtra(Intent.EXTRA_TEXT,"Your message here...");
startActivity(intent);
Edit 2
val selectorIntent = Intent(Intent.ACTION_SENDTO)
selectorIntent.data = Uri.parse("mailto:")
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("recipient#mail.com"))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject here...")
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email Body...")
emailIntent.selector = selectorIntent
activity!!.startActivity(Intent.createChooser(emailIntent, "Send email..."))
// This is for Gmail App
Intent email= new Intent(Intent.ACTION_VIEW);
email.setType("message/rfc822")
.setData(Uri.parse("mailto:your.email#gmail.com"))
.putExtra(Intent.EXTRA_EMAIL, "your.email#gmail.com")
.putExtra(Intent.EXTRA_SUBJECT, "Subject")
.putExtra(Intent.EXTRA_TEXT, "My Email message")
.setPackage("com.google.android.gm");
startActivity(email);
I m using this to launch gmail app.
val intent: Intent? = activity.packageManager.getLaunchIntentForPackage("com.google.android.gm")
if (intent != null) {
startActivity(intent)
}
else{
showToast("Sorry...You don't have gmail app")
}
//This is open with gmail
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setType("text/plain");
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Mail Subject");
i.putExtra(Intent.EXTRA_TEXT , "massage");
i.setPackage("com.google.android.gm");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(AnotherActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
I am using this
Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
startActivity(mailClient);
you can also try this
final Intent intent = new Intent(Intent.ACTION_VIEW)
.setType("plain/text")
.setData(Uri.parse("test#gmail.com"))
.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail")
.putExtra(Intent.EXTRA_EMAIL, new String[]{"test#gmail.com"})
.putExtra(Intent.EXTRA_SUBJECT, "test")
.putExtra(Intent.EXTRA_TEXT, "hello. this is a message sent from my demo app :-)");
startActivity(intent);
use for plenty of emails:
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test#gmail.com" });
for single emails:
intent.setData(Uri.parse("test#gmail.com"));

Error When Trying To Submit EditText Form To My Email? No Apps Can Perform This Action?

Here is the Error Message...
"Class 'Anonymous class derived from OnClickListener' must either be
declared abstract or implement abstract method 'onClick(View)' in
'OnClickListener'"
Here is the source code, anybody have an idea on how I can fix this error?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donate);
Button donationButton = (Button) findViewById(R.id.donation_submit_button);
donationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(DonateActivity.this, PostDonationActivity.class));
}
});
donationButton.setOnClickListener(new View.OnClickListener() {
public void onClick() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{
"ethan.reinsch#fillmorecentral.org"
});
i.putExtra(Intent.EXTRA_SUBJECT, "Test Subject");
i.putExtra(Intent.EXTRA_TEXT, "Test Body");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(DonateActivity.this, "There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
}
});
All help is very much appreciated!! I am fairly new to Android Studio, and Android App Development in General. Thank You!
UPDATE/EDIT:
I got it to work, but when I click submit, it says no apps can perform this action? I am using an emulator. Will it be different on an actual device? Thanks!
donationButton.setOnClickListener(new View.OnClickListener() {
// here is problem
#Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{
"ethan.reinsch#fillmorecentral.org"
});
i.putExtra(Intent.EXTRA_SUBJECT, "Test Subject");
i.putExtra(Intent.EXTRA_TEXT, "Test Body");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(DonateActivity.this, "There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
}
});
something like this, note SENDTO
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto: " + EMAIL.getText().toString()));
i.putExtra(Intent.EXTRA_SUBJECT, editText.getText().toString());
try {
startActivity(Intent.createChooser(i, ""));

Trying to send file via E-mail

I am trying to send my CSV file via e-mail in my app. When i click send it show the person i'm sending to, the subject, the message and the attached file, but when it sends the phone says "It wasnt possible to show the attached file". When i check my e-mail box, the message is there but without the file.
String to=destinatario.getText().toString().trim();
String subj=subject.getText().toString().trim();
String msg=message.getText().toString().trim();
if(to.length() < 1)
{
Toast.makeText(getApplicationContext(), "Mete para quem quer mandar", Toast.LENGTH_LONG).show();
}
else if (subj.length() < 1) {
Toast.makeText(getApplicationContext(), "Introduza o Tema", Toast.LENGTH_LONG).show();
}
else if (msg.length() < 1) {
Toast.makeText(getApplicationContext(), "Introduza Mensagem", Toast.LENGTH_LONG).show();
}
else {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
//emailIntent.setType("image/jpeg");
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{to});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subj);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getAbsolutePath()));
startActivity(Intent.createChooser(emailIntent, "A enviar..."));
}
}
Does anyone know why this happens??
Change your extra stream Uri.parse String to "file://" + file.getAbsolutePath().
This should work as long as your file is the correct file
I use the following code to create an intent with a csv attachment.
ArrayList<Uri> uriList = new ArrayList<Uri>();
ArrayList<String> fileNameList = new ArrayList<String>();
uriList.add(Uri.fromFile(f));
fileNameList.add(f.getName());
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[]{""});
emailIntent.putExtra(android.content.Intent.EXTRA_CC,
new String[]{""});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Log");
if (!uriList.isEmpty()) {
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
emailIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, fileNameList);
}

How to stop multiple mails at one time?

I am trying to send email with intent, but it is going with multiple mail id's because I am using multiple email id's in my android device. So I want to send the email with only one email id, at one time.
For example:- I am using two mail id's xxx#gmail.com and yyy#gmail.com.
So my code is sending mail by default with both of mail id's.
protected void sendEmail() {
String[] TO = {"zzz#gmail.com"};
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, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
I got the answer, it was my big mistake. I was using following code two times. That's why it was sending email randomly two times.
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();

Categories

Resources