I have function send mail and it looks like this
private void email(String emailTo, String subject, String emailText, ArrayList<DummyItem> lstItemsUpload) {
// need to "send multiple" to get more than one attachment
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{ emailTo});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
// has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
// convert from paths to Android friendly Parcelable Uri's
for (int i = 0; i < lstItemsUpload.size(); i++) {
MyItem myItem = lstItemsUpload.get(i);
File fileIn = new File(myItem.getPath());
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
If i choose Email application in chooser, it's will get default mail address and place it in from text box.
Is there any way to put the from mail address in the code, so the email application will use the input mail address instead of default mail address
I don't think you can change the default from mail since the mail client is linked to the user's mail address.
Related
I have an ecommerce app that shares the product as html document. Using this code I can send the product.html as attachment. I want to send the html document as the body of email instead of attachment.
Here is my code :
private void sendEmail(Context context, String emailTo, String emailCC,
String subject, String emailText, List<String> filePaths) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType(TEXT_HTML);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailTo});
emailIntent.putExtra(android.content.Intent.EXTRA_CC, new String[]{emailCC});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
ArrayList<Uri> fileUris = new ArrayList<Uri>();
for (String file : filePaths) {
File fileIn = new File(file);
Uri uri = Uri.fromFile(fileIn);
fileUris.add(uri);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris);
context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.send_by_email))); }
Anything that needs to go into the body can be set into your intent this way e.g. puts your builds version name as the body of the email. You can replace that with your html content. Have never tried that personally, You might need some conversion.
emailIntent.putExtra(Intent.EXTRA_TEXT, BuildConfig.VERSION_NAME);
In Android Studio I wish to send an email on a button click. I am using the following code till I work out what is going on before I start changing thing.
String[] TO = {"ABC#yahoo.com.au"};
String[] CC = {"xyz#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, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Some message added in here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
This works fine and well and shows up on my phone with the email content being as expected, however the email content, "Some message added in here" line is clearly hardcoded. I obviously wish to add in my own content by doing the following
String content = "Information I want to send";
emailIntent.putExtra(Intent.EXTRA_TEXT, content);
But for some reason the email content is blank. Why does is a string "content" recognised but a String variable x is not?
Check This Examples
By Looking at your code i only found problem in setting
emailIntent.setType(text/plain).
May Be you are using Gmail for sending mails(So you have to check second example).
Send Email (to Phone Email Client)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some#email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
Send Email (to Gmail)
Gmail does not examine the extra Intent fields, so in order to use this intent, you need to use the Intent.ACTION_SENDTO and pass a mailto: URI with the subject and body URL encoded.
String uriText =
"mailto:youremail#gmail.com" +
"?subject=" + Uri.encode("some subject text here") +
"&body=" + Uri.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"));
I would like to attach string as html attachment in android mail,how can I achieve this.Kindly help me with a snippet to do this.Thanks a lot.
Use intent to send email like below way .. and if u want to add some string as attachment u have to save that content into some directory with .html ext and later u can add it as attachment into email intent. [This code for idea it may need few modification as per ur app requirement..]
String emailText = "email message";
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "demo#gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hi");
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
String attachContent = "<html> content </html>";
/* this method u have to write, to save the string into html file
into cache or any other dir */
saveFile(getCacheDir()+"/attach.html", attachContent );
File file = new File(root, getCacheWebDir()+"/attach.html");
Uri uri = Uri.fromFile(file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Here is my code It does sends the mail but without any Image as attachment
EditText eto=(EditText)findViewById(R.id.etxtTo);
EditText esub=(EditText)findViewById(R.id.etxtSubject);
String to=eto.getText().toString();
String sub=esub.getText().toString();
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/image");
//emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//String filePath = ImageDetailActivity.this.getResources().getAssets()+"/"+file;
//Log.d("file address",filePath);
//Log.d("file address method2","file:///android_assets/"+file);
File ff=new File(filePath);
pngUri=Uri.fromFile(new File("file:///android_assets/"+file));
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, sub);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.parse("file:///android_assets/"+file));
startActivity(Intent.createChooser(emailIntent, "Choose an Email client :"));
The problem here is I am not able to receive Images attached with the mail.
I have tried a lot of method serching for the solution but not able to figure out the problem
I want to select a number of email addresses and then send an email to all of them.
My code is as below:
emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{listofemailaddresses});
emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, Constants.SMS_MESSAGE);
this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));`
listofemailaddresses is a string which contains all the emails separated by a ',' sign. But the To field is always empty in this.
Add this line to your code:
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "appsupport#YOUR_DOMAIN.com" });
This will fill the "To" section of your screen.
If you having the list of email addresses seprated by , then split that string to get individual email id as follow:
String [] emailList = emailAddresses.split(",");
now use emailList with your Intent.EXTRA_EMAIL key,as this will show all email addresses inside to field of send email form.
How about this code:
final Intent emailLauncher = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailLauncher.setType("message/rfc822");
emailLauncher.putExtra(Intent.EXTRA_EMAIL, emailList);
emailLauncher.putExtra(Intent.EXTRA_SUBJECT, "check this subject line");
emailLauncher.putExtra(Intent.EXTRA_TEXT, "hey check this message body!");
try{
startActivity(emailLauncher);
}catch(ActivityNotFoundException e){
}
Intent intent = null;
intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL,new String[] { "abc#gmail.com" , "test#gmail.com", "xyz#test.com"});
startActivity(intent);