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:"));
Related
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String emailList[]= {"rufidennis#gmail.com"};
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL,emailList);
intent.putExtra(Intent.EXTRA_SUBJECT,"Email Subject");
intent.putExtra(Intent.EXTRA_TEXT,"file:///android_asset/www/sample3.html");
startActivity(Intent.createChooser(intent,"Choice email APP"));
}
});
I am using buttons onclick listener method to send an email via my app. I have an html file which i want to send as an email content, the location of file is stored in www folder inside assets. How can i send that email as an email content??
Use the below code to sent a mail with file attachement
String filename="file.html";
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType(TEXT_HTML);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,emailTo);
emailIntent.putExtra(android.content.Intent.EXTRA_CC,emailCC);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT,"file:///android_asset/www/sample3.html");
context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.send_by_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..."));
So from my Android app I can send emails with attachment on Gmail. On outlook looks like it is attaching the file (.txt), but when I open the mail there is no attached file.
This is my code:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
Uri uriFileToShare = Uri.fromFile(file);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, file.getName());
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, uriFileToShare);
this.startActivityForResult(Intent.createChooser(emailIntent, activity.getString(R.string.send)+" "+file.getName()+" "+activity.getString(R.string.by_email)),code);
I have tried different solutions but no result.
The file of course exists and is not empty. As I said, on Gmail is correctly attached.
Any idea?
This code worked for me on outlook file attachment.
public static void emailLog(Context context) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
String filePath = fileDir + "/" + fileName;
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
File recordingFile = new File(filePath);
Uri fileUri = Uri.fromFile(recordingFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
context.startActivity(Intent.createChooser(emailIntent, "Some text..."));
}
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 am trying to send an email with an attachment using intent.
the file is a pdf stored in the raw folder. it is less than 200k.
but it shows file too large to attach. Y?
help me pls..
Intent in = new Intent(Intent.ACTION_SEND);
in.setType("application/pdf");
in.putExtra(Intent.EXTRA_EMAIL,new String[]{email.getText().toString()});
in.putExtra(Intent.EXTRA_SUBJECT, "Android Development Course");
Uri uri = Uri.parse("android.resource://deepak.android.samples/raw/android");
in.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(in, "Sending email.."));
use this method
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[]{"emailaddress"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Body");
String rawFolderPath = "android.resource://" + getPackageName()
+ "/" + R.raw.shortcuts;
// Here my file name is shortcuts.pdf which i have stored in /res/raw folder
Uri emailUri = Uri.parse(rawFolderPath );
emailIntent.putExtra(Intent.EXTRA_STREAM, emailUri);
emailIntent.setType("application/pdf");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));