向用户发送自动电子邮件

时间:2020-09-30 13:17:19

标签: java mysql sendmail

我想每月(第28天)发送一次自动电子邮件。在本地该方法有效并且已发送电子邮件,但是当我在服务器上部署该应用程序时,发送电子邮件的方法不起作用,因此该电子邮件不会发送给用户。
我不知道出什么问题了?

注意:云上的prod数据库(putty)。
该应用程序可以在prod上运行,并且没有出现任何问题,但是发送电子邮件的方法不起作用,因为它不发送电子邮件

   public static void main(String[] args) throws Exception {
    
            SpringApplication.run(SpringBootHelloWorldApplication.class, args);
    
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/...","name database","password");
            Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
            int day = calendar.get(Calendar.DATE);
            
            if(day == 28) {
            Statement ccf = con.createStatement();
            ResultSet ddj = ccf.executeQuery("SELECT gestion_cra.employe.email FROM gestion_cra.employe");
                  while(ddj.next()) {
                    String mel = ddj.getString("email");
                    JavaMailUtil.senMail(mel);  
                 }
                  
           } 

sendMail方法:

public static void senMail(String recepiant) throws MessagingException {
    Properties properties = new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "send.one.com");
    properties.put("mail.smtp.port", "587");
    String MyAccountEmail = "mail";
    String Password ="password";
    Session session = Session.getDefaultInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(MyAccountEmail, Password);
        }
    });
            
    Message message = prepareMessage(session, MyAccountEmail, recepiant);
    
    Transport.send(message);
        
}

application.properties:

jwt.secret=....//
jwt.get.token.uri=/login

spring.datasource.url = jdbc:mysql://localhost:3306/...
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto = update
spring.datasource.username = //name of database
spring.datasource.password = //password
server.address=0.0.0.0

0 个答案:

没有答案