3

I am trying to send an e-mail using the Javamail, since it seems to be the only way to send an e-mail using SMTP.

I searched some posts like: Sending mail in android without intents using SMTP

But, I want a sender to be my own domain, not the gmail.

So I tried:

    final String username = "admin@customsite.com";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.customsite.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("admin@customsite.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("username@gmail.com"));
            message.setSubject("Hello there");
            message.setText("This is a test email");
            Transport.send(message);
            Log.d("EmailClient", "Success");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
    }

But it seems not to be working.

Mium
  • 303
  • 1
  • 2
  • 12
  • Would sending the email from a server in the cloud be an option/of interest to you? The way I have done it is to just use a Java web application which can be reached via a REST call from the Android app. – Tim Biegeleisen Jul 10 '18 at 11:00
  • @TimBiegeleisen Well, I thought sending an e-mail using Javamail was simple, I must look at how to send mail using REST? – Mium Jul 10 '18 at 11:07
  • No, what I was suggesting is that if you write a server side program/script, you can send mail by any method you want. – Tim Biegeleisen Jul 10 '18 at 11:09
  • @TimBiegeleisen If you mean using a PHP or something like that, I am afraid I am extremely weak about passing an email address to server and sending an email using that data. – Mium Jul 10 '18 at 11:20
  • @mium so have you installed a smtp in your server? – Joaquín Jul 10 '18 at 11:24
  • @TimBiegeleisen yes – Mium Jul 10 '18 at 11:32
  • @Mium what does "not working" mean? What debugging have you done? How does it fail? What does the [JavaMail debug output](https://javaee.github.io/javamail/FAQ#debug) show? – Bill Shannon Jul 10 '18 at 17:08

1 Answers1

3

I've done the same a week ago, with a custom smtp here's my code

Class SendMail.JAVA , I have Password and Mail Account in Config.JAVA, Take note that my server was running on port 25 and didn't have SSL

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;

//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,"Enviando mail","Espere por favor...",false,false);
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //Dismissing the progress dialog
    progressDialog.dismiss();
    //Showing a success message
    Toast.makeText(context,"Mail enviado",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", "Custom SMTP");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "25");

    //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, "utf-8", "html");

        //Sending email
        Transport.send(mm);

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

}

Example implementation in Activity

private void sendEmail() {
        //Getting content for email
        String email = asd.getText().toString();
        String subject = "Presupuesto plaza hogar";
        String message = body.toString();

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

        //Executing sendmail to send email
        sm.execute();
    }
Joaquín
  • 1,116
  • 1
  • 10
  • 26
  • If SendMail handles ProgressDialog, how can I handle the next process in activity? I think I have to get return value, but I get no idea – Mium Jul 10 '18 at 11:32
  • @Mium Explain more please, I really don't know what you're triying to do :P – Joaquín Jul 10 '18 at 11:34
  • I mean I implied sm.execute in activity, how can this activity knows the email sending is complete? (I think I have to get return value like true and false) – Mium Jul 10 '18 at 11:37
  • @mium in sendmail.java handle the exception in the try catch, just do this } catch (MessagingException e) { e.printStackTrace(); Your Toast Message HERE } if the mail still not beign delivered and you have no error in your code then you have a problem on your server side – Joaquín Jul 10 '18 at 11:42
  • I mean I want to use ProgressBar (since Progressdialog is deprecated) so I designed ProgressBar in my activity. If the Mail process is complete, I want to disable ProgressBar like: progressbar.setVisibility(View.INVISIBLE); To do that, I have to identify if the process is completed or sending in progress. – Mium Jul 10 '18 at 11:50
  • @mium ahh not i got you :P, yea, just make a boolean in sendmail.java and set it to true only if it completes the process, and set it to false in the exception, then just read the value from your activity – Joaquín Jul 10 '18 at 11:53
  • Thanks a lot. It reallty did help me to boost progress – Mium Jul 10 '18 at 14:20