ERROR: this - cannot be Applied to

Asked

Viewed 67 times

0

error appears in the part of the code where I have "this"

private void sendEmail() {
    //Getting content for email
    String email = editTextEmail.getText().toString().trim();
    String subject = editTextSubject.getText().toString().trim();
    String message = editTextMessage.getText().toString().trim();

    //Creating SendMail object
    SendMail sm = new SendMail(this,email,subject,message);

    //Executing sendmail to send email
    sm.execute();
}

my Sendmail class is this

public class SendMail 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 message;

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



    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

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

    @Override
    public 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;
    }
}
  • The Sendmail class has no constructor with this signature: SendMail(Activity, String, String, String)

  • Then the constructors can be generated automatically, but I get errors in the Message method

  • Can explain what would be automatically generated constructors?

  • Because it seems to be syntax error, as there is some examples.

1 answer

2

On the line:

SendMail sm = new SendMail(this,email,subject,message); 

You are trying to call a constructor that does not exist. You must create one that accepts the same parameters that are being passed:

public class SendMail 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 message;

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

    public SendMail(Context context, String email, String subject, String message) {
        this.context = context;
        this.email = email;
        this.subject = subject;
        this.message = message;
    }

    ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.