如何使用Java向多个用户发送邮件

问题描述

我想向所有用户发送自动电子邮件(每个第一个月),我按照Google上的示例进行操作,最后,我可以发送电子邮件,但是我必须手动编写用户的电子邮件,但我不知道该如何做。恢复所有用户的电子邮件(我不掌握JAVA)。 如何阅读所有用户电子邮件(在main类中)和感谢信。
错误

线程“ main”中的异常java.lang.NullPointerException在 com.javainuse.service.UserService.findAll(UserService.java:17)位于 com.javainuse.SpringBoothelloWorldApplication.main(SpringBoothelloWorldApplication.java:31)

UserService.java:

@Autowired
    private UserRepository userRepository;

    public List<User> findAll() {
        var it = userRepository.findAll(); //Line 17
        var users = new ArrayList<User>();
        it.forEach(e -> users.add(e));
        System.out.println("message__"+ users);
        return users;
    }

JavaMail.java:

public static void senMail(String recepiant) throws MessagingException {
        System.out.println("preparer mail");
        //System.out.println(User.class);
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        int day = calendar.get(Calendar.DATE);
        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 = "xxxxxxxx@gmail.com";
        String Password ="xxxxx";
        
        Session session = Session.getDefaultInstance(properties,new Authenticator() {
            @Override
            protected PasswordAuthentication getpasswordAuthentication() {
                return new PasswordAuthentication(MyAccountEmail,Password);
            }
        });
        
        Message message = prepareMessage(session,MyAccountEmail,recepiant);
        
        
        if(day == 1) {
            Transport.send(message);
            System.out.println("mail sending");
        }
        
    }

    private static Message prepareMessage(Session session,String MyAccountEmail,String recepiant) {
        
            try {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(MyAccountEmail));
                message.setRecipient(Message.RecipientType.TO,new InternetAddress(recepiant));
                message.setSubject("tst A");
                message.setText("testestestestestestestestestestest");
                return message;
            } catch (AddressException e) {
                // Todo Auto-generated catch block
                e.printstacktrace();
            } catch (MessagingException e) {
                // Todo Auto-generated catch block
                e.printstacktrace();
            }
            
        
        return null;
    }

SpringBoothelloWordApplication.java:

public static void main(String[] args) throws Exception {
        
        SpringApplication.run(SpringBoothelloWorldApplication.class,args);
        
        var tst = (new UserService()).findAll();  //Line 31
        
        System.out.println("message__"+ tst.size());
        
        JavaMailUtil.senMail("fdr@gmail.com");
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)