Send mail with javamail, show Exception

1

Hello to everyone who reads, as the title says I need to send mail with javamail, I have already implemented it and it works super well, the problem is with the exeption, because I use AsyncTask it always tells me to send even if there is not sent the mail, my question how can I adjust the following code to truly know the status of the mail (Sent or not).

Here goes the code (SendMail Class)

package apk.cjam.im;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
import java.util.Properties;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
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;

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

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

//Information to send email

private String subject;
private String message;

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

//Class Constructor

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

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //Showing progress dialog while sending email
    progressDialog = ProgressDialog.show(context,"Enviando Mensaje","Por favor espere...",false,false);
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //Dismissing the progress dialog
    progressDialog.dismiss();
    //Showing a success message
    //Toast.makeText(context,"Mensaje Enviado",Toast.LENGTH_LONG).show();

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.setMessage("Se ha enviado la petición al servidor, revise su cliente de correo dentro de 1 minutos aproximadamente  y estará la respuesta a su petición ");
    builder.setNeutralButton("OK",null);
    builder.setTitle("Petición enviada con éxito\n" +
            "\n");
    builder.create().show();
}

@Override
protected Void doInBackground(Void... params) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    final String user_nauta = pref.getString("user_nauta","");
    final String user_pass  = pref.getString("pass_nauta","");
    final String mailto = pref.getString("mailto","");
    final String server_smtp  = pref.getString("server_smtp","");
    final String port_smtp = pref.getString("port_smtp","");
    Properties props = new Properties();
    props.put("mail.smtp.host", server_smtp);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", port_smtp);
    session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                //Authenticating the password
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user_nauta, user_pass);
                }
            }); 
    try { 
        MimeMessage mm = new MimeMessage(session);
        mm.setFrom(new InternetAddress(user_nauta));
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(mailto));
        mm.setSubject(subject);
        mm.setText(message);
        Transport.send(mm);

    } catch (MessagingException e) {
        e.printStackTrace();
    }
    return null;
}
}
    
asked by cjamdeveloper 07.03.2017 в 20:55
source

0 answers