Search code examples
androidemailgmailjakarta-mailstack-overflow

Sending an e-mail without using the built-in app


I need to send an e-mail with attachments automatically without user input. I previously read these 2 answers.

Sending Email in Android using JavaMail API without using the default/built-in app

Android: Send e-mail with attachment automatically in the background.

But now I don't receive any e-mail and I already enabled the less secure apps in gmail. Here's my code :

public class Mail {
private String mailhost;
private String user;
private String password;
private Session session;
private String serverport;

public void main(String user, String password, String subject, String body){


    sendFromGMail(user, password, subject,body);
}

private void sendFromGMail(String from, String pass, String subject, String body){
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getInstance(props);
    MimeMessage message = new MimeMessage(session);
    try {
        InternetAddress adressTo = new InternetAddress(from);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, adressTo);

        message.setSubject(subject);
        message.setText(body);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    catch(MessagingException e){
        Log.e("Not Sent", e.getMessage(), e);
    }
}

}

And this is how I use it :

 try {
            Mail mail = new Mail();
            mail.main(gmailusername, gmailpassword, "Test", "Test2");
            this.publishProgress("Email Sent");
        }catch(Exception e){
            Log.e("SendMail", e.getMessage(), e);
            this.publishProgress("Email not sent");
        }

Edit : I don't want to use the built-in app as the title suggested. Just wanted to make it clear.

Java debug :

08-12 15:08:41.152  24062-24340/com.documax.cardreader I/System.out﹕ DEBUG: setDebug: JavaMail version 1.4.1
08-12 15:08:41.162  24062-24340/com.documax.cardreader I/System.out﹕ DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1]
08-12 15:08:41.162  24062-24340/com.documax.cardreader I/System.out﹕ DEBUG SMTP: useEhlo true, useAuth true
08-12 15:08:41.162  24062-24340/com.documax.cardreader I/System.out﹕ DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false

Solution

  • Fix all these common mistakes, including getting rid of the Authenticator.

    Get rid of the call to super() in your constructor, you don't need it.

    Also, get rid of the ByteArrayDataSource class. You don't need your own since it comes with JavaMail. Plus, your program isn't even using it.

    If it still doesn't work, update your post with your new code and new failure details.