Send email through App without opening mail App in Android

Viewed 2090

so I searched a lot to send an email throught my app, but without the user having to log in in an email app and send it himself. I would like to just let him write it in an editText and then just press a button and send it to me. So that's what I did: 2 classes for the mail thing, my activity which calls it, add the 3 libraries and the internet permission. What did I do wrong?

Here is where I call the email process :

private View.OnClickListener btnMode1Listener = new 
View.OnClickListener() {
    @Override
    public void onClick(View v) {
        suggestionText = entre_suggestion.getText().toString();

        Log.i("SendMailActivity", "Send Button Clicked.");

        String fromEmail = "fromEmail@gmail.com";
        String fromPassword = "frompassword";
        String toEmails = "toEmail@gmail.com";
        List toEmailList = Arrays.asList(toEmails
                .split("\\s*,\\s*"));
        Log.i("SendMailActivity", "To List: " + toEmailList);
        String emailSubject = "Suggestion";
        String emailBody = suggestionText;
        new SendMailTask(suggestions.this).execute(fromEmail,
                fromPassword, toEmailList, emailSubject, emailBody);

    }
};

This is the first class "GMail":

public class GMail {

final String emailPort = "587";// gmail's smtp port
final String smtpAuth = "true";
final String starttls = "true";
final String emailHost = "smtp.gmail.com";

String fromEmail;
String fromPassword;
List toEmailList;
String emailSubject;
String emailBody;

Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;

public GMail() {

}

public GMail(String fromEmail, String fromPassword,
             List toEmailList, String emailSubject, String emailBody) {
    this.fromEmail = fromEmail;
    this.fromPassword = fromPassword;
    this.toEmailList = toEmailList;
    this.emailSubject = emailSubject;
    this.emailBody = emailBody;

    emailProperties = System.getProperties();
    emailProperties.put("mail.smtp.port", emailPort);
    emailProperties.put("mail.smtp.auth", smtpAuth);
    emailProperties.put("mail.smtp.starttls.enable", starttls);
    Log.i("GMail", "Mail server properties set.");
}

public MimeMessage createEmailMessage() throws AddressException,
        MessagingException, UnsupportedEncodingException {

    mailSession = Session.getDefaultInstance(emailProperties, null);
    emailMessage = new MimeMessage(mailSession);

    emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
    for (Object toEmail : toEmailList) {
        Log.i("GMail","toEmail: "+toEmail);
        emailMessage.addRecipient(Message.RecipientType.TO,
                new InternetAddress((String) toEmail));
    }

    emailMessage.setSubject(emailSubject);
    emailMessage.setContent(emailBody, "text/html");// for a html email
    // emailMessage.setText(emailBody);// for a text email
    Log.i("GMail", "Email Message created.");
    return emailMessage;
}

public void sendEmail() throws AddressException, MessagingException {

    Transport transport = mailSession.getTransport("smtp");
    transport.connect(emailHost, fromEmail, fromPassword);
    Log.i("GMail","allrecipients: "+emailMessage.getAllRecipients());
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
    transport.close();
    Log.i("GMail", "Email sent successfully.");
}

}

And this is the second class "SendMailtask":

public class SendMailTask extends AsyncTask {

private ProgressDialog statusDialog;
private Activity sendMailActivity;

public SendMailTask(Activity activity) {
    sendMailActivity = activity;

}

protected void onPreExecute() {
    statusDialog = new ProgressDialog(sendMailActivity);
    statusDialog.setMessage("Getting ready...");
    statusDialog.setIndeterminate(false);
    statusDialog.setCancelable(false);
    statusDialog.show();
}

@Override
protected Object doInBackground(Object... args) {
    try {
        Log.i("SendMailTask", "About to instantiate GMail...");
        publishProgress("Processing input....");
        GMail androidEmail = new GMail(args[0].toString(),
                args[1].toString(), (List) args[2], args[3].toString(),
                args[4].toString());
        publishProgress("Preparing mail message....");
        androidEmail.createEmailMessage();
        publishProgress("Sending email....");
        androidEmail.sendEmail();
        publishProgress("Email Sent.");
        Log.i("SendMailTask", "Mail Sent.");
    } catch (Exception e) {
        publishProgress(e.getMessage());
        Log.e("SendMailTask", e.getMessage(), e);
    }
    return null;
}

@Override
public void onProgressUpdate(Object... values) {
    statusDialog.setMessage(values[0].toString());

}

@Override
public void onPostExecute(Object result) {
    statusDialog.dismiss();
}

}

And finally, the 3 librairies I added:

compile files('libs/activation.jar')
compile files('libs/additionnal.jar')
compile files('libs/mail.jar')

So yeah, I searched a lot and didn't find how to debug it. I tried a lot of different ways but it never sent the email. I just want to be clear that I don't want to open the mail app, I want the email to be sent without the user having to do something.

2 Answers
Related