首页 > 解决方案 > 向用户发送自动电子邮件

问题描述

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

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

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

方法发送邮件:

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

应用程序属性:

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

标签: javamysqlsendmail

解决方案


推荐阅读