1
votes

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

2

2 Answers

4
votes

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.

1
votes

Try this

Intent i = new Intent(Intent.ACTION_SEND);  
i.setType("message/rfc822") ; // use from live device
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});  
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."));