0

I am making an app that sends email to my group when the user will press submit button without opening gmail but the app crashes just after the screen in the pic:-

enter image description here

This is where I declare what happens when the button is pressed:-

Button submit=(Button) findViewById(R.id.rSubmit);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String email,subject,message;
            email="the_email_id_of_receiver";
            subject="Test";
            message="This is a test";
            SendMail sm = new SendMail(RequestBlood.this, email, subject, message);
            sm.execute();
        }
    });

The is the class that handles the task of sending mail:-

package org.bloodconnect.bloodconnect;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


//Class is extending AsyncTask because this class is going to perform a networking operation
public class SendMail extends AsyncTask<Void,Void,Void> {

private Context context;
private Session session;

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

//Progressdialog to show while sending email
private ProgressDialog progressDialog;

//Class Constructor
public SendMail(Context context, String email, String subject, String message){
    //Initializing variables
    this.context = context;
    this.email = email;
    this.subject = subject;
    this.message = 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
    progressDialog.dismiss();
    //Showing a success message
    Toast.makeText(context,"Message Sent",Toast.LENGTH_LONG).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(Config.EMAIL, Config.PASSWORD);
                }
            });

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

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

        //Sending email
        Transport.send(mm);

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

}

This is the class where I declare my id and password:-

package org.bloodconnect.bloodconnect;

public class Config {
    public static final String EMAIL ="my_email_id";
    public static final String PASSWORD ="password";
}

As it can be seen in the pic, unitl the onPreExecute method in SendMail class, everything is running fine (I hope so). I am pretty new to android so please help me fix this error. Thanks in advance :)

Logcat is in 2nd pic.

Logcat

user207421
  • 305,947
  • 44
  • 307
  • 483
  • https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jul 28 '18 at 16:18
  • It did help me to know that error is in doInBackground method, but what after that? –  Jul 28 '18 at 16:29
  • 1
    show us the logcat... from which u come to know that error is in doInBackground – Sahdeep Singh Jul 28 '18 at 16:47
  • @SahdeepSingh- edited my question to show Logcat. Please check that. Thanks –  Jul 28 '18 at 16:54
  • Have you checked your email? Maybe some security messages have been sent – Evgeniy Jul 28 '18 at 17:02
  • Your error states that the class definition is not known. You must add the necessary dependencies to the gradle file! Take a look at the answer where it describes which dependencies to add :: https://stackoverflow.com/questions/49786771/sending-email-android-using-javamail-api – Barns Jul 28 '18 at 17:03
  • @Evgeniy - no, i have not received any mail –  Jul 28 '18 at 17:10
  • @Barns - I have already added much more dependencies than what the answer said, still no help :( –  Jul 28 '18 at 17:12
  • Show what you have added to the gradle file. – Barns Jul 28 '18 at 17:15
  • Link to pic- https://ibb.co/fcdZDo –  Jul 28 '18 at 17:19
  • @Barns - any help? Please! –  Jul 29 '18 at 15:14

2 Answers2

0

Use the official JavaMail for Android release, not the old forked version referenced in the other answer.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
-1

There's an Android friendly version of Java Mail. This avoids the indirect dependency on Java AWT, which is not included in the Android runtime.

More details here.

df778899
  • 10,703
  • 1
  • 24
  • 36