2

hi first of all i want to say an using gmail sender To send mails on a button click in my app where i got Solution From Here

now in the above code i cant attach files .but later i have seen a solution from Stack over flow to solve this in that there was some modification done in the "GMailSender.java" file the modified link is Here

how ever there is Addition portion to attach file now my problem is that i don't understand the modified portion. here is the old portion

public synchronized void sendMail(String subject, String body, String sender, String recipients)

the modified portion is

public synchronized void sendMail(String subject, String body, String sender, String recipients, File attachment)

this is not a duplicated question i just want to know What this "File attachment" portion is and what type or method should i implement to attach a file if you have any doubt please go through these Two links and please find the solution thanks in advance

Community
  • 1
  • 1
Ramz
  • 7,116
  • 6
  • 63
  • 88

2 Answers2

4

You're passing a File object called attachment. That will be whatever you want to attach to your email.

You would attach it to your email message like:

MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);

MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);

MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment); //set attachment to filedatasource
mbp2.setDataHandler(new DataHandler(fds)); //add the filedatasource object to your 2nd mimebodypart
mbp2.setFileName(fds.getName());

Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1); 
mp.addBodyPart(mbp2);

message.setContent(mp);
...send email...

Edit: Never had to get an image from the SD card, but I think you can easily create a File object of it like this:

File imageFile = new File("path to image on sd card");

Then you would call your sendMail method passing in that file object.

bschultz
  • 4,244
  • 1
  • 25
  • 33
  • i dont understand that how to can i attach a jpeg file – Ramz Feb 07 '12 at 16:03
  • Where are you pulling the jpeg from? – bschultz Feb 07 '12 at 16:06
  • a picture file from mycomputer to the virtual memory card on my emulator – Ramz Feb 07 '12 at 16:12
  • how will i specify the file that has been pull to the mnt – Ramz Feb 07 '12 at 16:13
  • bschultz did you get my answer for your question bro please help – Ramz Feb 07 '12 at 16:20
  • So you're getting the image from the SD card and need to know how to set it to your File object? – bschultz Feb 07 '12 at 16:25
  • my image on mnt its is gg.jpg how will i specify the path is there any particular method to implement the path – Ramz Feb 07 '12 at 16:36
  • 1
    Here's a good tutorial on Data Storage in Android http://developer.android.com/guide/topics/data/data-storage.html#filesExternal That should give you some insight on how to get the path to your image. I know File extStore = Environment.getExternalStorageDirectory(); will give you the path to the SD card. – bschultz Feb 07 '12 at 16:44
  • 3
    File imageFile = new File("/sdcard/ss.jpg"); this is the methord by which we specifty the path thankyou once again well done – Ramz Feb 07 '12 at 17:25
1

Try this

Intent i = new Intent(Intent.ACTION_SEND);  
i.setType("message/rfc822") ; // use from live device
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});  
i.putExtra(Intent.EXTRA_SUBJECT,"subject goes here");  
i.putExtra(Intent.EXTRA_TEXT,"body goes here");  
i.putExtra(Intent.EXTRA_STREAM, new File(""));
startActivity(Intent.createChooser(i, "Select email application."));
Felipe Conde
  • 2,024
  • 23
  • 26