Skip to content Skip to sidebar Skip to footer

Attachment In Gmail Using Code

i have the working code for sending email using gmail account, Now I just want to go for attachment using code without user interaction.

Solution 1:

publicsynchronizedvoidsendMail(String subject, String body, String sender, String recipients, File attachment)throws Exception {
    try{
    MimeMessagemessage=newMimeMessage(session);
    message.setSender(newInternetAddress(sender));
    message.setSubject(subject);

    MimeBodyPartmbp1=newMimeBodyPart();
    mbp1.setText(body);

    MimeBodyPartmbp2=newMimeBodyPart();
    FileDataSourcefds=newFileDataSource(attachment);
    mbp2.setDataHandler(newDataHandler(fds));
    mbp2.setFileName(fds.getName());

    Multipartmp=newMimeMultipart();
    mp.addBodyPart(mbp1);
    mp.addBodyPart(mbp2);

    message.setContent(mp);

    if (recipients.indexOf(',') > 0)
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
    else
        message.setRecipient(Message.RecipientType.TO, newInternetAddress(recipients));
    Transport.send(message);
    }catch(Exception e){

    }
}

Solution 2:

If you want to send some files from SDCard, please use these codes:

publicstaticvoidemail(Context context, String emailTo, String emailCC,
    String subject, String emailText, List<String> filePaths)
{
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = newIntent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
        newString[]{emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, 
        newString[]{emailCC});
    //has to be an ArrayListArrayList<Uri> uris = newArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri'sfor (String file : filePaths)
    {
        File fileIn = newFile(file);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

Android Intent for sending email with attachment

Android multiple email attachments using Intent

Trying to attach a file from SD Card to email

http://www.anddev.org/code-snippets-for-android-f33/sending-email-with-attachment-using-intent-t11041.html

http://www.toxicbakery.com/android-development/creating-emails-android/

http://android-geek.blogspot.be/2011/04/share-via-email-intent-image-attachment.html

Solution 3:

I suppose you are not using any third party library to send mails.

Intenti=newIntent(Intent.ACTION_SEND);
        i.setType("image/jpg");
        i.putExtra(Intent.EXTRA_EMAIL, newString[] {"someone@gmail.com"} );
        i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/myImage.gif"));
        startActivity(i);

Post a Comment for "Attachment In Gmail Using Code"