0

I have to classes the main class plus JavaMailUtl class This is my JavaMailUtl class:

import javax.mail.Authenticator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class JavaMailUtl {
    public static void sendMail(String recepient) throws Exception{
        System.out.println("Preparing Email");
        Properties properties = new Properties();

        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");

        String myAccountEmail = "someEmail@gmail.com";
        String password = "xxxxxxxx";

        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                return new javax.mail.PasswordAuthentication(myAccountEmail, password);
            }

});
        Message message = prepareMessage(session, myAccountEmail, recepient);
        Transport.send(message);
        System.out.println("Message sent!");
    }

    private static Message prepareMessage(Session session, String myAccountEmail, String recepient){
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(myAccountEmail));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
            message.setSubject("Gmail API");
            message.setText("This Message sent from Java APP");
        } catch (Exception ex) {
            Logger.getLogger(JavaMailUtl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}

and this is my main class:

public class JavaEmailMessaging {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        JavaMailUtl.sendMail("someOtherEmail@gmail.com");
    }

}

When I try to run the project I get the following error:

Preparing Email
Exception in thread "main" java.lang.NullPointerException
    at javax.mail.Transport.send(Transport.java:123)
    at mailing.email.messaging.JavaMailUtl.sendMail(JavaMailUtl.java:43)
    at mailing.email.messaging.JavaEmailMessaging.main(JavaEmailMessaging.java:18)

Doubled check the code lots of times and found no problem for me every object I have is pointing to the right place.

3Bady
  • 85
  • 1
  • 11
  • 1
    Google security blocks any NON-Google attempt to access gmail. How to enable: see this:https://devanswers.co/allow-less-secure-apps-access-gmail-account/ –  Feb 15 '20 at 14:26

1 Answers1

1

Your prepareMessage method returns null. Define message before the try - and then return it.

Something like this:

    private static Message prepareMessage(Session session, String myAccountEmail, String recepient) {
        Message message = null;
        try {
            message = new MimeMessage(session);
            message.setFrom(new InternetAddress(myAccountEmail));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
            message.setSubject("Gmail API");
            message.setText("This Message sent from Java APP");
        } catch (Exception ex) {
            Logger.getLogger(JavaMailUtl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return message;
    }

Hope that helps.

EDIT: Also, it's worth noting that an enhancement in Java 14 should make this situation less painful. You'll get more info on what exactly was null.

andrewjames
  • 8,705
  • 5
  • 10
  • 31
  • Thanks for your reply, your code unfortunately didn't work for me and thank you for making my life easier .. – 3Bady Feb 15 '20 at 18:50
  • Can you elaborate? Do you still get the exact same null pointer exception, or something new/different? – andrewjames Feb 15 '20 at 19:00
  • `Exception in thread "main" javax.mail.MessagingException: Could not convert socket to TLS;` `at mailing.email.messaging.JavaMailUtl.sendMail(JavaMailUtl.java:40)` line 40 where `Transport.send(message);` is – 3Bady Feb 15 '20 at 20:13
  • OK - so that's a different problem from the one in your original question. You have fixed the `null pointer exception` problem. Your code looks good to me. However, the TLS error suggests some other configuration issue in your environment or on your machine - I do not get that error when I run your code. For example, take a look at [this question](https://stackoverflow.com/questions/16115453/javamail-could-not-convert-socket-to-tls-gmail/20773077) (specifically for things like antivirus software causing a problem). But be careful - some of the recommended solutions are dangerous. – andrewjames Feb 15 '20 at 20:45
  • And also, remember to follow the advice from @Voodoo above. – andrewjames Feb 15 '20 at 20:46