Monday, 19 September 2011

Sending Email

We can send email in android with the help of Android Intent with user interface and also without promt to user.

First we will see how can we send email with mail interface:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent .setType("plain/text");
emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"abc@pqr.com"});
emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, mySubject);
emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, myBodyText);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+ FileName));  // Attachment

context.startActivity(Intent.createChooser(emailIntent, "Send mail...));




Second way:

We can also send mail to user without providing the mail interface;
This can be done by JavaMail API implemented for Andorid . We can also send html content, attachment.

Hope the given demo code will help you :)

Mail sendMail = new Mail("My_User_ID", "PASSWORD"); // Replace user id and password
//according  to your credential
String[] toArray = {"1st mail id", "2nd mail id"};
sendMail.setTo(toArray);
sendMail.setFrom("USER EMAIL");
sendMail.setSubject("This is an email sent using Mail JavaMail wrapper from an Android device.");
sendMail.setBody("Email body contents");
try {
sendMail.addAttachment("/sdcard/imagefolder/myImage.png"); // Image path for attachment
if(sendMail.send()) {
Toast.makeText(this, "Email was sent successfully.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Email was not sent.", Toast.LENGTH_SHORT).show();
}
} catch(Exception e) {
Log.e(TAG, "Could not send email", e);
}

No comments:

Post a Comment