Can a server and Java servlet send email without user interaction?

I searched all over Google and stackoverflow about whether the server can send automatic email, for example, every Monday, using the servlet without any user interaction. At the moment, the user must log in and the codes will be used up. What I want is a server that issues code at a specific time, and the user does not need to log in and not even see this. I do not want to relay users, I want to relay on the server.

+5
source share
5 answers

You need to have a non-contextual thread on your server. Ideally, this thread should be started using the ServletContextListener to be sure that it will be active even before the first client connection.

You can either create something from scratch, or use a special tool, for example, an excellent Quartz scheduler , which will make the boiler plate code for you.

Once you have a scheduler, you just need to send mail to the mail server via SMTP. And here you can implement the protocol manually (SMTP is not so difficult), but if you do not want to reinvent the oval wheel where there are round wheels, use the javamail API from the Java EE SDK or another third-party library, such as the one provided by Apache

+3
source

While your server application is running, you can use Quartz to schedule the process for sending emails.

Check here: https://quartz-scheduler.org/documentation

+2
source

You can accomplish what you need by setting up another application that runs as a service. At the bottom, this app can use something like Quartz to manage scheduled events.

You can synchronize it with a web application using some database. Such an approach should include automation of email transmission.

+2
source

You can use Quartz to schedule tasks that will run on a specified date and time. There are two types of triggers in Quartz 2

  • SimpleTrigger - this Allows you to set the start time, end time, repetition interval.

  • CronTrigger - Allows a Unix cron expression to specify the dates and times of your work.

You can get the Quartz library from the official website or from the maven repository.

Home solution

If you do not want to use any third-party jar for this purpose, there is another way. In J2SE 1.3, Java contains java.util.Timer and java.util.TimerTask classes that can be used for this purpose.

see also

+1
source

Background streams

You must have a background thread and wait for the next email. Java 5 adds some classes to make such background threads and tasks pretty easy.

Some other answers mentioned a quartz library. I have not used this, and it may have nice features. But this is certainly not necessary for your purpose. Classes built into Java are enough.

ServletContextListener

Create a subclass of ServletContextListener . This class must be called before processing any servlet request. So this is the place to initialize the objects used by your web application. This class is also called when your web application closes.

ScheduledExecutorService

In your servlet context listener, when your web application starts, create a ScheduledExecutorService . This class uses a thread pool to run tasks in the background. You specify the initial delay before the first run (optional) and indicate how often to repeat the task.

In your servlet context listener, be sure to turn off your ScheduledExecutorService when your web application shuts down. If you do not, the background threads in the pool will continue to live. The java process running on your host operating system to start your servlet container will continue rather than exit.

Be sure to add try-catch around your task. Any exception that reaches your ScheduledExecutorService causes the service to silently stop. See this funny post (cautious, mischievous language).

Search StackOverflow for more details and examples. This question is basically a duplicate, so I saved this report.

+1
source

Source: https://habr.com/ru/post/1232955/


All Articles