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..."));
Related
I am trying to send an email with an attachment, I have created a pdf file as well as a text file, for attaching a text file and sending an email i am using this code
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String path = "/ScriptEmails/"+ filename;
String filepath = Environment.getExternalStorageDirectory()+File.separator+"Screenwriter"+File.separator+path;
sendEmail ("text", "enter email here", "Script from scriptwrite android", "Your script is attached", filepath);
And the send email function is like this:
public void sendEmail (String attachmentType, String emails, String subject, String text, String filePath)
{
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType(attachmentType);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emails});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+filePath/*mnt/sdcard/Myimage.jpeg"*/));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
now, the path is correct, the file name is correct, yet upon sending an email, it says, the attachment couldn't be send.
What i think is that i am not putting right ATTACHMENT TYPE for my file type.
What would be the attachment type for a text file and what would be an attachment type for a pdf file?
for text file i am using (txt/plain) is this correct?
Try this code for attach file with email client.
File file = new File(path);
Uri pngUri = Uri.fromFile(file);
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{});
emailIntent.putExtra(android.content.Intent.EXTRA_BCC,new String[]{});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
Path should be like below.
final String path = Environment.getExternalStorageDirectory().toString()+ "/.....your_path";
For attaching the PDF you need to set the type as "application/pdf" and for text file "text/plain" below :
File externalStorage = Environment.getExternalStorageDirectory()+File.separator+"Screenwriter"+File.separator+path;
Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "yourfilename.pdf"));
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Text");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email using:"));
I am trying to attach an image file and .txt file but only one file is attaching one is not attaching how to attach both the files.
if i use ACTION_SEND_MULTIPLE then the app gets force close.
HERE IS MY CODE:
public static Intent getSendEmailIntent(Context context, String email,
String subject, String body, String fileName) {
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
// Explicitly only use Gmail to send
emailIntent.setClassName("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail");
emailIntent.setType("*/*");
// Add the recipients
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,
new String[] { email }
);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
// Add the attachment by specifying a reference to our custom
// ContentProvider
// and the specific file of interest
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ "anything.txt"));
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ "anything.jpg"));
return emailIntent;
}
If I put a .jpg file at last before return it is attaching. But if I put a .txt file at last then it is attaching. I want to attach both files pls help.
You can just use the code below and change file path's to your files.
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(Uri.fromFile(new File("/mnt/sdcard/Medplann/IMG.jpg"))); // Add your image URIs here
imageUris.add(Uri.fromFile(new File("/mnt/sdcard/Medplann/Report.pdf")));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));
Read this page : send
What is the type i can use for intent for sending an email with an html page attachment? Whatever type i use, the sent mail contains an empty page as an attachment
Here's some code
String root = "/data/data/com.email/files/";
String fileName = "Payslip.html";
if (true) {
attachment = new File(root, fileName);
}
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("application/octet-stream");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { address.getText().toString() });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject.getText());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
emailtext.getText());
emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(attachment));
EmailActivity.this.startActivity(Intent.createChooser(
emailIntent,
"Send mail..."));
As for now, we can use context.getFilesDir() to retrieve a File object to the "files" directory. With that you can also get another File object to your stored HTML page. You can also use the File.exists() and File.isFile() method for checking. Thanks for Christian Lisching
I have to following problem:
I want to send an email with an image attached to it.
I wrote this code:
File file = context.getDir("Files", context.MODE_WORLD_WRITEABLE);
File image = new File(file, "image.jpg");
Uri U = Uri.fromFile(image);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpg");
i.putExtra(Intent.EXTRA_STREAM, U);
context.startActivity(Intent.createChooser(i, "Email:"));
The email is sent but there is no attachment.
Does anybody have any idea why the email is sent without the attachment?
EDIT
I have found the answer to my question. Because the image was stored on the internal storage, it did not have enough rights so it could not be sent via email. I've moved my image to externalStorage and now it's working :)
Thanks,
Ark
String smsBody = "Body of the Content";
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/*");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject of the Mail");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, smsBody);
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile("mnt/sdCard/SampleImageFloder/TestImage.png"));
emailIntent.setType("vnd.android.cursor.dir/email");
activity.startActivity(Intent.createChooser(emailIntent,"Email:"));
Try this one -
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{"email"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Test");
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(emailIntent);
Just see my Existing Answer also
I am sending a pdf file in email attachment, attachment is going successfully in email and it is showing me in email as attachment, but when i download this pdf it having no content i.e. it's size is zero byte.
Am I missing some permission? or Any solution regarding this?
Here is my code
private void emailSend(String emailString, String subject, String pdfPath){
String[] email = { emailString };
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"MY body");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pdfPath));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
Finally i got the solution to my problem from this post.
I use
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(pdfPath)));
instead of
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pdfPath));
it resolved my issue.
Thanks
I've done this for sending any file from SD card with mail attachment...
Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.setType("rar/image");
sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new
File("/mnt/sdcard/download/abc.rar")));
startActivity(Intent.createChooser(sendEmail, "Email:"));