Android\Intent: Send an Email with Image Attachment

Android\Intent: Send an email with image attachment

Try below code...

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..."));

How to send email with Attachment(Image)

Here is something that can help you. Make sure you spelled your image file path in a proper manner. Don't forget the "/" separator (try to get a log of your path). Also, be sure that the file exists.

/** ATTACHING IMAGE TO EMAIL AND SENDING EMAIL  */
Button b1 = (Button)findViewById(R.id.finalsectionsubmit);
b1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

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:"));
eraseContent();
sentMode = true;
}
});

Android: How to send an image as email attachment from application?

public class MainActivity extends Activity {
Button send;
Bitmap thumbnail;
File pic;
EditText address, subject, emailtext;
protected static final int CAMERA_PIC_REQUEST = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send=(Button) findViewById(R.id.emailsendbutton);
address=(EditText) findViewById(R.id.emailaddress);
subject=(EditText) findViewById(R.id.emailsubject);
emailtext=(EditText) findViewById(R.id.emailtext);
Button camera = (Button) findViewById(R.id.button1);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"fake@fake.edu"});
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
//Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + " " + pic.exists());
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
i.setType("image/png");
startActivity(Intent.createChooser(i,"Share you on the jobing"));
}
});

}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
pic = new File(root, "pic.png");
FileOutputStream out = new FileOutputStream(pic);
thumbnail.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}

}
}

How to send mail with an image as an attachment in android?

 public class MailImageFile extends javax.mail.Authenticator {

public MailImageFile(){}

public void Mail(String user, String pass) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc@gmail.com", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@gmail.com"));
message.setContent(_multipart);
message.setSubject("Testing Subject");
message.setContent("Hi...", "text/html; charset=utf-8");

Transport.send(message);

} catch (MessagingException e) {
throw new RuntimeException(e);
}

//Got this solution form here

    private Multipart _multipart; 
_multipart = new MimeMultipart();

public void addAttachment(String filename,String subject) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);

BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(subject);

_multipart.addBodyPart(messageBodyPart2);
}

}

Android - Attach image to E-mail

Create a globale Uri variable then save the uri in onActivityResult()

Uri uri = null;

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
uri = selectedImage; // here set the uri
String PathP = selectedImage.getPath().toString();
FilePathPreview.setText(PathP);
}
}

then pass the same uri variable to

        //....
email.putExtra(Intent.EXTRA_STREAM, uri);
//....
//need this to prompts email client only
email.setType("message/rfc822");

startActivity(Intent.createChooser(email, "Choose an Email client :"));

Update: Add this flag if you still cant share

email.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

Send Image with email Intent not displaying

    String recepientEmail = ""; // either set to destination email or leave empty
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recepientEmail));
intent.putExtra(Intent.EXTRA_SUBJECT,"email subject");
intent.putExtra(Intent.EXTRA_TEXT,message);
intent.putExtra(Intent.EXTRA_STREAM,filePath);
startActivity(intent);

How to capture image and send its Uri directly to attachment via Email

Here After a lot of research, I have found the solution in the Android documentation

https://developer.android.com/reference/android/support/v4/content/FileProvider.html#GetUri

Here is my final code for sharing a captured image to Gmail.

this code will go straight to Manifest file you have to use authorities as a valid authority provider.

 <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/picker_provider_paths" />
</provider>

XML file for the provider:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="image_picked" path="picked/"/>
<external-path name="*" path="Pictures/"/>
</paths>

Function to SendImage as an attachment.

    private fun sendEmail() {
val contentUri =
FileProvider.getUriForFile(context!!,
"yourPackageAndThen.com.mydomain.fileprovider",
File(PATH OF IMAGE or FILE ));

val emailIntent = Intent(Intent.ACTION_SEND)

//need this to prompts email client only
emailIntent.type = "message/rfc822";
val to = arrayOf("EMAIL ID TO SEND")
emailIntent.putExtra(Intent.EXTRA_EMAIL, to)
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

// attachment Uri comes through camera
emailIntent.putExtra(Intent.EXTRA_STREAM, contentUri)

// the mail subject
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report")
//email body
emailIntent.putExtra(Intent.EXTRA_TEXT, MESSAGEforBody)
startActivity(Intent.createChooser(emailIntent, "Send email using..."))

}


Related Topics



Leave a reply



Submit