Sure, here is how to send an email without the inbuilt Gmail app and with an attachment in Android:
Code snippet
public void sendEmail(String[] recipients, String subject, String body, Uri attachment) {
// Create an intent to send an email
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
// Set the recipients
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
// Set the subject
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
// Set the body
intent.putExtra(Intent.EXTRA_TEXT, body);
// Set the attachment
if (attachment != null) {
intent.putExtra(Intent.EXTRA_STREAM, attachment);
}
// Start the activity to send the email
startActivity(intent);
}
This code will first create an intent to send an email. The intent will be set to send a
message/rfc822 type of email.
The putExtra() method is used to set the values of the intent's extras. The extras are key-value pairs that are used to pass data to the activity that is started by the intent.
The EXTRA_EMAIL extra is used to set the recipients of the email. The
EXTRA_SUBJECT extra is used to set the subject of the email. The
EXTRA_TEXT extra is used to set the body of the email. The EXTRA_STREAM extra is used to set the attachment of the email.
The startActivity() method is used to start the activity to send the email. The activity that is started will be the default email app on the device.
To use this code, you would need to call the sendEmail() method in your app. For example, you could call the method in your app's
onCreate() method.
Here is an example of how you could call the sendEmail() method in your app's
onCreate() method:
Code snippet
public void onCreate() {
super.onCreate();
// Get the recipients
String[] recipients = {"recipient1@example.com", "recipient2@example.com"};
// Get the subject
String subject = "This is an email";
// Get the body
String body = "This is the body of the email";
// Get the attachment
Uri attachment = Uri.parse("path/to/attachment.jpg");
// Send the email
sendEmail(recipients, subject, body, attachment);
}
When you run the app, the default email app on the device will be started and the email will be sent.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is how to send an email without the inbuilt Gmail app and with an attachment in Android:
Code snippet
This code will first create an intent to send an email. The intent will be set to send a
message/rfc822type of email.The
putExtra()method is used to set the values of the intent's extras. The extras are key-value pairs that are used to pass data to the activity that is started by the intent.The
EXTRA_EMAILextra is used to set the recipients of the email. TheEXTRA_SUBJECTextra is used to set the subject of the email. TheEXTRA_TEXTextra is used to set the body of the email. TheEXTRA_STREAMextra is used to set the attachment of the email.The
startActivity()method is used to start the activity to send the email. The activity that is started will be the default email app on the device.To use this code, you would need to call the
sendEmail()method in your app. For example, you could call the method in your app'sonCreate()method.Here is an example of how you could call the
sendEmail()method in your app'sonCreate()method:Code snippet
When you run the app, the default email app on the device will be started and the email will be sent.