0

I want to add an attachment (be it a link,image, video or anything else) along with the text that I send through email. Till now, I can only send the plain text as email. How do I add an attachment along with it?

Here's my code for sending the email process in the worker thread:

public class GMailSender extends AsyncTask<Void,Void,Void> {

//Declaring Variables
private Context context;
private Session session;

//Information to send email
private String email;
private String subject;
private String msg;

//Progressdialog to show while sending email

//Class Constructor
public GMailSender(Context context, String email, String subject, String msg){
    if (rb1 != null && rad.isChecked()){
        message=s1;
    }else if(rb1 != null && rad1.isChecked())
    {
        message=item;
    }
    //Initializing variables
    this.context = context;
    this.email = s4;
    this.subject = s3;
    this.msg = message;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //Showing progress dialog while sending email
   // progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //Dismissing the progress dialog
    //Showing a success message
    Toast.makeText(context,"Message Sent",Toast.LENGTH_SHORT).show();
}

@Override
protected Void doInBackground(Void... params) {
    //Creating properties
    Properties props = new Properties();

    //Configuring properties for gmail
    //If you are not using gmail you may need to change the values
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    //Creating a new session
    session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                //Authenticating the password
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(Smscreator.EMAIL, Smscreator.PASSWORD);
                }
            });

    try {
        //Creating MimeMessage object
        MimeMessage mm = new MimeMessage(session);

        //Setting sender address
        mm.setFrom(new InternetAddress(Smscreator.EMAIL));
        //Adding receiver
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //Adding subject
        mm.setSubject(subject);
        //Adding message
        mm.setText(msg);

        //Sending email
        Transport.send(mm);

    } catch (MessagingException e) {
        e.printStackTrace();
    }
    return null;
}
}

Here's the code for sending the mail :

GMailSender sm = new GMailSender(context, s4, s3, message);
    sm.execute();
    Toast.makeText(context, "Email sent :)",
            Toast.LENGTH_SHORT).show();

Edit : As suggested, I've looked into [Sending Email in Android using JavaMail API without using the default/built-in app. But nothing is written about adding an attachment in the solutions.

  • 1
    Possible duplicate of [Sending Email in Android using JavaMail API without using the default/built-in app](https://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a/5787716#5787716) – Tigger Mar 24 '19 at 05:19
  • Read the ["second answer"](https://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a/5787716#5787716) - which is about how to add an attachment. There is [another example](https://stackoverflow.com/questions/8970455/java-mail-sending-multiple-attachments-not-working/8970497). There is a [_break down_ of email which might help](https://stackoverflow.com/questions/13331989/how-to-handle-multipart-alternative-mail-with-javamail/16931800) as well. – Tigger Mar 24 '19 at 06:23

1 Answers1

0

Fix all these common JavaMail mistakes. Make sure you're using the JavaMail for Android. And see the JavaMail FAQ for how to send a message with an attachment. For example, see the sendfile.java sample program.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • My app keeps showing a white screen and the email is never sent when I implemented this sendfile.java code. Please help –  Mar 25 '19 at 16:39
  • Are you using sendfile.java unchanged? Are you running it from the command line? Did you read the source code to understand how to use it? If you want to use it in an Android application you'll need to extract the relevant parts and include them in your application. If you've already done that and your application still isn't working, update your original post with the new code and post the [JavaMail debug output](https://javaee.github.io/javamail/FAQ#debug). – Bill Shannon Mar 26 '19 at 05:56