title: Schedule sending emails in Spring boot
body: <p>I would like to schedule sending email notifications using Spring boot, the user creates the notification(Subject, Message, Sending date...)</p>
  <pre><code>public class EmailNotification implements Serializable {
      
      /**
       * 
       */
      private static final long serialVersionUID = 3400952201530474821L;
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
      private String subject;
      private String message;
      @Column(name = &quot;sending_date&quot;)
      @Basic(fetch = FetchType.EAGER)
      private String sendingDate;
  </code></pre>
  <p>Normally, I get a list of notifications from the DB, and I should send each notification using the loop &quot;for&quot; with its own informations (specified by the client), but I found a problem with the sending time, I couldn't use the @scheduled annotation because the cron expression here should be dynamic(retrieved from the DB)
  Here is my method:</p>
  <pre><code>public void periodicNotification()  {
          JavaMailSenderImpl jms = (JavaMailSenderImpl) sender;
          MimeMessage message = jms.createMimeMessage();
          MimeMessageHelper helper;
          try {
              helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, StandardCharsets.UTF_8.name());
              
          //get the list of notifs from the DB
          List&lt;EmailNotification&gt; emailNotifs = emailNotificationsRepo.findAll();
  
          for (EmailNotification i : emailNotifs) 
          {
              helper.setFrom(&quot;mail&quot;);
              List&lt;String&gt; recipients = fileRepo.findWantedEmails(i.getDaysNum());
  
              String[] to = recipients.stream().toArray(String[]::new);
              helper.setTo(to);
              helper.setText(i.getMessage());
              helper.setSubject(i.getSubject());
              sender.send(message);
              System.out.println(&quot;Email successfully sent to: &quot; + Arrays.toString(to));
          }
          }
          catch (MessagingException e) {
              
              e.print...
          }
  
      }
  </code></pre>
  <p>I started this method:</p>
  <pre><code>    public static void main(String[] args) {
          try {
              JobDetail job1 = JobBuilder.newJob(Job1.class).withIdentity(&quot;job1&quot;, &quot;group1&quot;).build();
  
              Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity(&quot;cronTrigger1&quot;, &quot;group1&quot;)
                      .withSchedule(CronScheduleBuilder.cronSchedule(&quot;0/30 * * * * ?&quot;)).build();
  
              Scheduler scheduler1 = new StdSchedulerFactory().getScheduler();
              scheduler1.start();
              scheduler1.scheduleJob(job1, trigger1);
  
              Thread.sleep(100000);
  
              scheduler1.shutdown();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  <p>But I want to schedule the sending of the email at the time specified by the client, in the method above, the cron expression is static, and that's not what I'm looking for</p>