Sending an email with android not sending one - android

I am trying to send an email to a user with Android which will include a code. For whatever reason, it does not work.
I have followed this tutorial
What is the problem?
This is the code:
public class PWReset extends AppCompatActivity {
EditText getMail;
Button Continue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pwreset);
getMail=findViewById(R.id.Mail);
Continue=findViewById(R.id.PWR2);
Continue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String Code = UUID.randomUUID().toString().substring(0, 5);
//After checking if the email is registered in the DB
resetPassword(getMail.getText().toString(), Code);
Intent intent = new Intent(PWReset.this, PWReset2.class);
intent.putExtra("code", Code);
startActivity(intent);
}
});
}
public void resetPassword(String Mail, String code){
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{getMail.getText().toString()});
email.putExtra(Intent.EXTRA_SUBJECT, "Password reset");
email.putExtra(Intent.EXTRA_TEXT, "You have requested a password reset.\nHere is the code for resetting your password:" + code +
"\nIf you have not requested a password reset, ignore this message.");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "gmail"));
}
}

Related

How to implement email intent correctly

I searched stack overflow and the google documentation for email intents and found that code to be the go to code:
However, everytime I click "submit" in my activity I choose gmail. It opens a new email in gmail, but it only puts the receipient address. The subject and the text(name, email address and feedback text) are missing.
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);
}
}
I implemented that as the following code. Everything after the else is to have a AlertDialog pop up if there is no app installed.
public class EmailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acitvity_email);
final EditText nameField = (EditText) findViewById(R.id.editTextName);
final EditText addressField = (EditText) findViewById(R.id.editTextEmail);
final EditText subjectField = (EditText) findViewById(R.id.editTextSubject);
final EditText feedbackField = (EditText) findViewById(R.id.editTextFeedback);
final Button submitFeedback = (Button) findViewById(R.id.buttonSubmitFeedback);
final String name = nameField.getText().toString();
final String address = addressField.getText().toString();
final String subject = subjectField.getText().toString();
final String feedback = feedbackField.getText().toString().concat(name).concat(address);
submitFeedback.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
composeFeedback(subject, feedback);
}
});
}
public void composeFeedback(String subject, String feedback){
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + "mydeveloperemail#gmail.com"));
intent.putExtra(Intent.EXTRA_TEXT,feedback);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
final Drawable fail = getResources().getDrawable(R.drawable.ic_fail);
AlertDialog.Builder builder = new AlertDialog.Builder(EmailActivity.this);
builder.setTitle("Fehler");
builder.setIcon(fail);
builder.setMessage("Keine Email App verfügbar!");
builder.setCancelable(false);
builder.setNeutralButton("Okay", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Edit: deleted intent filter as #CommonsWare pointed out in his answer
ACTION_SENDTO is not required to use any extras. Put your data in the mailto: Uri, the way a Web page would.
In the android manifest i declared an intent filter to the activtiy
Unless you are writing an email app, like Gmail, this <intent-filter> is both unnecessary and damaging to users.

How to send Zip file from sdcard to mail in android

Hi friends,
I need to send zip file as attachment to email. I have tried using code below. However, I receive email message without the attachment. What am I doing wrong?
public class MainActivity extends Activity {
Button email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email=(Button)findViewById(R.id.button1);
email.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"radha#impressol.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Email Message");
intent.setType("application/zip");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://+/sdcard/zipname.zip"));
startActivity(Intent.createChooser(intent, "Send Email"));
}
});
}
}
In your case remove the "+" from Uri string.
P.S. it'll also work without Uri protocol, like (Environment.getExternalStorageDirectory()+"/zipname.zip");

Can't add email signature in Android

I'm trying to compose and send an email which will include a signature at the bottom
of my email content in android. I'm able to send an email but I'm not getting the way that allows me to add my own signature. Do you have any suggestion?
here's my code:
public void addListener() {
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findViewById = (TextView) findViewById(R.id.t1);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { "some#gmail.com"});
/*i.putExtra(Intent.EXTRA_CC,
new String[] { "some#gmail.com" });*/
i.putExtra(Intent.EXTRA_SUBJECT, "Android Test");
i.putExtra(Intent.EXTRA_TEXT, "Body");
//i.putExtra(Intent.EXTRA_TEXT, "signature");
try {
startActivity(Intent.createChooser(i, "Choose mail app..."));
} catch (android.content.ActivityNotFoundException ex) {
}
}
});
}
I don't think the "signature" is really a different section of an email. I think you can just append your signature to your body.
i.putExtra(Intent.EXTRA_TEXT, "Body" + "\\n" + "Signature");

Launch SMS using data from a scanned QR Code

I'm trying to start the SMS activity with the phone number from a scanned QR Code filled in. So far I've been able to get the activity to launch but the number and message fields are blank. Here's what I have, currently this is crashing the app:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
Uri uri = Uri.parse(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setType("vnd.android-dir/mms-sms");
i.setData(uri);
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
The String smsUri contains the scanned string from the QR Code, "SMSTO:666-666-1234:hello". How can I get the SMS Activity to launch with the phone number and the message already entered into the number and body fields?
I saw this post:
Sending SMS using Intent does not add recipients on some devices
Would I need to parse the QR Code result myself and break it into the phone number and message, then add those as Extras like that example?
Got it!
Since I can't answer my own question yet here it is:
Okay, got it to work. I made a class to break the QR result into separate elements:
public class Sms {
public static String[] breakString(String s) {
String[] smsElements = s.split(":");
return smsElements;
}
}
Then changed the method I have above to:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
String[] smsElements = Sms.breakString(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", smsElements[1]);
i.putExtra("sms_body", smsElements[2]);
i.setData(Uri.parse("smsto:" + smsElements[1]));
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
Okay, got it to work. I made a class to break the QR result into separate elements:
public class Sms {
public static String[] breakString(String s) {
String[] smsElements = s.split(":");
return smsElements;
}
}
Then changed the method I have above to:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
String[] smsElements = Sms.breakString(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", smsElements[1]);
i.putExtra("sms_body", smsElements[2]);
i.setData(Uri.parse("smsto:" + smsElements[1]));
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}

sending email in android

public class AndroidEmailActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittextEmailAddress = (EditText) findViewById(R.id.email_address);
final EditText edittextEmailSubject = (EditText) findViewById(R.id.email_subject);
final EditText edittextEmailText = (EditText) findViewById(R.id.email_text);
Button buttonSendEmail_intent = (Button) findViewById(R.id.sendemail_intent);
buttonSendEmail_intent.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String emailAddress = edittextEmailAddress.getText().toString();
String emailSubject = edittextEmailSubject.getText().toString();
String emailText = edittextEmailText.getText().toString();
String emailAddressList[] = { emailAddress };
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);
intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
intent.putExtra(Intent.EXTRA_TEXT, emailText);
startActivity(Intent.createChooser(intent,
"Choice App t send email:"));
}
});
}
}
This is my code.I am not getting any emails.I need to send emails through this intent method.how can I acheive that?Where I went wrong?pls give some suggestions.
I am getting the error when I click the Send email button as "No applications can perform this action."
This is my code to send emails:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.share_email_subject));
sendIntent.setType("message/rfc822");
activity.startActivity(Intent.createChooser(sendIntent, "Choose Email Client"));
Try removing the plain/text type?
It is Good practice to work with original device rather than on Virtual Device. I suggest you try this code block on real device.
Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.setType("plain/text");
startActivity(Intent.createChooser(sendEmail, "Email:"));
If you want to see more on Email with Intent then refer to this post:
http://androidtutforbeginner.blogspot.com/2012/03/send-email-with-intent-in-android.html

Categories

Resources