我必须对主类和JavaMailUtl类进行分类
这是我的JavaMailUtl类:

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 = "[email protected]";
        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;
    }
}



这是我的主要课程:

public class JavaEmailMessaging {

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

}


当我尝试运行项目时,出现以下错误:

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)


仔细检查代码很多次,发现每个对象指向正确的位置对我来说都没有问题。

最佳答案

您的prepareMessage方法返回null。在try之前定义消息-然后返回它。

像这样:

    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;
    }


希望能有所帮助。

编辑:另外,值得注意的是,an enhancement in Java 14应该可以减轻这种情况的痛苦。您将获得有关确切为空的更多信息。

关于java - Transport.send在javax.mail库上不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60239371/

10-11 02:42