How to add an attachment to an email? - android

I have this email sending code that does not show a chooser (good). How can I extend it to include a file attachment, without involving a chooser?
// Based on http://stackoverflow.com/questions/3312438
final Intent intent = new Intent(Intent.ACTION_VIEW);
final Uri data = Uri.parse("mailto:user#domain.com?subject=My Sugbject&body=");
intent.setData(data);
startActivity(intent);

try this :
Intent emailintent = new Intent(android.content.Intent.ACTION_SEND);
emailintent.setType("image/jpeg");
emailintent.putExtra(android.content.Intent.EXTRA_TEXT,
"email body here");
emailintent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]
{"test#gmail.com"});
emailintent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject here");
String filName="file:///sdcard/photos/estdemo.jpg";
emailintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+ filName));
this.startActivity(emailintent);

Try Following code,
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("image/jpg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("photo.jpg"));
startActivity(i);

Use the below code to sent a mail
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("vnd.android.cursor.dir/email");
String to[] = "asd#gmail.com";
sharingIntent.putExtra(Intent.EXTRA_EMAIL, to);
sharingIntent.putExtra(Intent.EXTRA_STREAM,filePath);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"subject");
startActivity(Intent.createChooser(sharingIntent, "Send email"));

Related

Find Solution to send email

I am creating utility to send email. In my code i use chooser intent to choose email app to send email. It work perfectly but the problem is that if i use attachment file in this code using Uri then in chooser i choose G-mail, and then G-mail is stopped. If i send email without attachment it work good. Can any one solve my problem. Here is my code.
public void SendEmail() {
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setData(Uri.parse("mailto: "));
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL,to);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
// intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send Mail..."));
}
In this code a line is in comment. if i use this line then G-mail is stopped. I am using this line for file attachment. Help me please.
/**Use the below Code Snippet**/
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("vnd.android.cursor.dir/email");
String to[] = {""};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + "Your URI"));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Use this code it's works for me correctly:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = {"info#marutinandan.com"};
//String aEmailCCList[] = { "user3#fakehost.com","user4#fakehost.com"};
//String aEmailBCCList[] = { "user5#fakehost.com" };
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
// emailIntent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList);
// emailIntent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "your subject");
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "your message body.");
startActivity(emailIntent);

Sending a contact .vcf file

i want to send a contact vcf file using the bluetooth and email. I have tried but i keep getting errors. Please help. Thanks
This is my sendByBluetooth method.
public void sendByBluetooth(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(storage_path) );
startActivity(intent);
}
This is my emailContact method.
public void emailContact() {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Add attributes to the intent
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,"subject line");
sendIntent.putExtra(Intent.EXTRA_TEXT,"Body of email");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(storage_path)));
sendIntent.setType("vnd.android.cursor.dir/email");
startActivity(Intent.createChooser(sendIntent,"Email:"));
}
Please Help! Thanks.
For bluetooth you can set the type as intent.setType("text/x-vcard");
public void sendByBluetooth(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/x-vcard");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(storage_path) );
startActivity(intent);
}
and for emails you could use
String filelocation="/mnt/sdcard/contacts.vcf";
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, filelocation);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
Just a side note: The.vcf file should be in in your SD card

android BCC is not working for email

emailIntent.putExtra(android.content.Intent.EXTRA_BCC,
new String[] {"postcards#in-touchmobile.com"});
I have tried with this code, but bcc can not include in my gmail application.
My full code is
Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(
Intent.EXTRA_EMAIL,
new String[] { Constants.EMAIL });
emailIntent.putExtra(
android.content.Intent.EXTRA_TEXT,
Html.fromHtml(str));
emailIntent.putExtra(
android.content.Intent.EXTRA_BCC,
new String[] { "postcards#in-touchmobile.com" });
emailIntent.putExtra(
android.content.Intent.EXTRA_SUBJECT,
Constants.Subject);
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(screenshotUri);
emailIntent.putParcelableArrayListExtra(
Intent.EXTRA_STREAM,
uris);
startActivity(emailIntent);
startActivity(emailIntent);
I have changed my code like above one..but still not working..
Try in this manner
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{strEmail});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/Myimage.jpeg"));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Must do this thing emailIntent.setType("application/image");
Checked and let me know
change the type of Intent from image/png and application/octet-stream to plain/text like
emainIntent.setType("plain/text");
and for attaching files to email see here

include attachment when sending email

my app is sending email with an attachment but the problem is it send the email but with no attachment. The file does exist in SDcard and i can detach it and view the file.
Here is the code Email.java
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
i.putExtra(Intent.EXTRA_SUBJECT, "");
i.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://mnt/sdcard/fun/itisfun.csv"));
i.putExtra(Intent.EXTRA_TEXT, "");
i.setType("message/rfc822");
startActivity(Intent.createChooser(i,"gmail :"));
Here are the permissions i have tried in Menifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="android.permission.INTERNET"></permission>
i will appreciate your help.
Try out as below :
File pngDir = new File(Environment.getExternalStorageDirectory(),"fun/");
if (!pngDir.exists())
{
pngDir.mkdirs();
}
File pngfile=new File(pngDir,"itisfun.csv");
Uri pngUri =Uri.fromFile(pngfile);
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{""});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Here is code I use to attach files to an email (Gmail app):
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[]{emailTo});
//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.putExtra(android.content.Intent.EXTRA_TEXT, emailText);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail via"));
Try this code:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailSignature);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, toSenders);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subjectText);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, messageText+"\n\n"+emailSignature);
emailIntent.setType("image/jpeg");
File bitmapFile = new File(Environment.getExternalStorageDirectory()+
"/"+FOLDER_NAME+"/picture.jpg");
myUri = Uri.fromFile(bitmapFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(emailIntent, "Send your email in:"));
Hope it will help you.
try the following code
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
i.putExtra(Intent.EXTRA_SUBJECT, "");
i.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://sdcard/fun/itisfun.csv"));
i.putExtra(Intent.EXTRA_TEXT, "");
i.setType("message/rfc822");
startActivity(Intent.createChooser(i,"gmail :"));

Email activity on Android

I want to send email from Android virtual machine to my gmail account.
Problem: but on pressing send button I am getting:
"No application can perform this action"
Here is my code:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("audio/mp3");
//sendIntent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
//sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/download/test.mp3"));
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(GlobalVariable.getstrEmail()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(sendIntent, "Title:"));
Try it on a real device itself, it should work. And you need to change the type:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"email#example.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "body text");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
This can help...
Intent openEmailIntent = new Intent(android.content.Intent.ACTION_SEND);
openEmailIntent.setType("plain/text");
openEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[{"zoombie#gmail.com"});
openEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"subject you want");
openEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Any body ");
this.startActivity(Intent.createChooser(openEmailIntent,"Sharing via Email"));
try this....
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailaddress});
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Categories

Resources