Adding attachments in java mail API android

I can send email by this code and email is send successfully
but the problem is that i want to attach picture to my email .

MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("myemailaddress@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(reciep));
            message.setSubject(subjct);
            message.setContent(txtmsg,"text/html; charset=utf-8");
            Transport.send(message);

Below is the method from which i can take picture and save it in myfolder .

 private void saveImage(byte[] arg0) {
 String root = Environment.getExternalStorageDirectory() + "/.MeraFolder";

File myDir = new File(root);
myDir.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fname = timeStamp + ".jpg";

file = new File(myDir, fname);
if (file.exists()) {
    file.delete();
}

Bitmap bitmap1 = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
FileOutputStream out;
try {
    out = new FileOutputStream(file);
    bitmap1.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

}

Now i have 2 questions
1) Is there a way that i can attach the recently taken picture in my email or is there any way that the picture i take is directly attached too my email .

2) When there is no internet connectivity the email gets failed ow can i over come this problem that when there is no internet connectivity the email gets on hold as soon as internet connectivity is available the email is sent .
Any help will be appreciated as i am new to android


Source: java

Leave a Reply