> For the complete documentation index, see [llms.txt](https://docs.poja.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.poja.io/examples/hello-world-but-with-scheduled-email-sending.md).

# Hello world, but with scheduled email sending

### Goal

We want to add **scheduled email sending** to an [Hello World Application](https://docs.poja.io/v1/docs/hello-world-but-straight-to-the-cloud) already hosted on Poja. Our goal is to **automatically send a daily email to a specified email address** using Poja’s Scheduled Tasks feature

{% hint style="info" %}
Deploy in one click

Before diving into the step-by-step guide, you can simply click the **Deploy to Poja** button. This will instantly deploy [this template](https://github.com/poja-app/poja-scheduled-mailing-template) to your Poja account

<a href="https://console.poja.io/applications/create/clone/?templateId=9e25599a-cf73-4558-8697-b22273a6171b" class="button primary">Deploy to Poja</a>
{% endhint %}

#### How-to in 2 steps

#### Step 1: Implement the email sending code

Because Scheduled Tasks are **asynchronous**, we first have to implement the email sending feature [as standard for asynchronous tasks](https://docs.poja.io/v1/docs/hello-world-but-with-asynchronous-reply-by-email).

Let’s update our configuration’s **Worker** to enable scheduled tasks:

<figure><img src="/files/VhpMUQ46uwE8ECQTazWN" alt=""><figcaption></figcaption></figure>

Next let’s create our Event class and event consumer:

{% code title="Java" %}

```java
package com.example.demo.endpoint.event.model;

import java.time.Duration;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@AllArgsConstructor
@Builder(toBuilder = true)
@Data
@EqualsAndHashCode(callSuper = false)
@ToString
public class EmailUpdateTriggered extends PojaEvent {
  @Override
  public Duration maxConsumerDuration() {
    return Duration.ofSeconds(20);
  }

  @Override
  public Duration maxConsumerBackoffBetweenRetries() {
    return Duration.ofMinutes(1);
  }
}
```

{% endcode %}

{% code title="Java" %}

```java
package com.example.demo.service.event;

import com.example.demo.endpoint.event.model.EmailUpdateTriggered;
import com.example.demo.mail.Email;
import com.example.demo.mail.Mailer;
import jakarta.mail.internet.InternetAddress;
import java.util.List;
import java.util.function.Consumer;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class EmailUpdateTriggeredService implements Consumer<EmailUpdateTriggered> {
  private final Mailer mailer;

  @SneakyThrows
  @Override
  public void accept(EmailUpdateTriggered emailUpdateTriggered) {
    var recipientAddress = new InternetAddress("your@email.com");
    mailer.accept(
        new Email(
            recipientAddress,
            List.of(),
            List.of(),
            "",
            "Here is your automatic daily email update !",
            List.of()));
  }
}
```

{% endcode %}

To trigger a deployment, just **commit and push** the code to the **preprod branch** of the repository.

#### Step 2: Make the email sending automatic everyday<br>

Now that we have our email sending code, all we have to do is to update our configuration using the **Scheduled Tasks** menu:

{% hint style="info" %}
Poja Scheduled Tasks configuration

* **Name**: the name of your scheduled task
* **Class name**: the **exact** class name of your asynchronous event, here `EmailUpdateTriggered`
* **Description**: what does your scheduled task do
* **Scheduled expression**: refers to the time or interval of execution of your scheduled task, refer to [this documentation](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-scheduled-rule-pattern.html) for further information, here `cron(0 6 * * ? *)` means **every day at 6:00 AM UTC**
* **Event stack source:** refers to the Queue and the Workers that will process your asynchronous event\
  If set to **2**, the following method must be overridden to return `your.package.name.endpoint.event.EventStack.EVENT_STACK_2` inside `EmailUpdateTriggered`

{% code title="Java" %}

```java
@Override
public EventStack getEventStack() {
  return EVENT_STACK_2;
}


```

{% endcode %}
{% endhint %}

<figure><img src="/files/j9aDCXLAshyZ7KLNVOgQ" alt=""><figcaption></figcaption></figure>

**Save** the configuration and wait for the deployment to complete, and voilà!

<br>

***
