StackTips
 1 minutes

How to Send an Email with Attachment in Android

By Nilanchala @nilan, On Sep 17, 2023 Android 2.34K Views

The following code snippet shows to send an email with image attachment in Android using the Intent method.

public static void sendEmail(Context context, String subject, String message, String imagePath) {
	Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
	emailIntent.setType("plain/text");
	emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
	emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
	
	if (imagePath != null && imagePath.length() > 0) {
		File file = urlToFile(imagePath);
		if (file != null && file.getAbsolutePath() != null)
			emailIntent.putExtra(Intent.EXTRA_STREAM, file.getAbsolutePath());
	}
	
	context.startActivity(emailIntent);
}
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.