# Schedule a recurring task to import POP3 content

## Overview

Monthly email newsletters are a tool for informing customers about your products and services. This information includes the latest product updates, new promotions, new tools, and upcoming corporate events. To stay informed, many organizations subscribe to competitor’s email newsletters to monitor their announcements and the quality of their marketing materials. These materials circulate internally and are used for strategic planning.

Sitefinity CMS can help you gather, aggregate, and publish this content to your Intranet. This tutorial guides you through the steps required to create a scheduled task that updates your feed automatically.\
For more information about feeds in Sitefinity CMS, see [Feeds](https://www.progress.com/documentation/sitefinity-cms/publishing-system-feeds).

## Subscribing 

Using the Sitefinity CMS publishing system, you can consume external RSS and ATOM feeds as well as mail content using the POP3 protocol. Once consumed, this data can be blended into a Publishing Point (data mashup) and published on your Intranet portal.

Once you aggregate the necessary information, you can choose either to publish this content in your Sitefinity CMS project as news, blogs, and events or push it directly to RSS.

## Create the feed

You can use the Sitefinity CMS publishing system to pull e-mails from a POP3 email account.

To  create a feed, set up a new POP3 import and configure its display in your website, perform the following:

### Create a feed

1. Go to Sitefinity CMS backend.
2. In the main menu, choose *Administration » Alternative Publishing*.
3. On the *Feeds & Notifications* page, click *Create a Feed*.\
   The *Create a feed* panel opens.
4. In the *Title* field, enter `Competitors Newsletters`.

### Configure the POP3 server

In the  *Content to include* section you define the content that goes to the Sitefinity CMS publishing point. It can be content from your Sitefinity CMS website or an external ATOM, RSS, or POP3 feed. To configure the feed to pull information from POP3, perform the following:

1. Click *Add another content type*.\
   The *Select content source panel* opens.
2. Select the option *Mail Content*.
3. In the *POP3 server*  field, enter your preferred mail server, typically `pop.gmail.com`.
4. In the *Port* field, enter `995`.
5. Choose whether to use SSL connection.\
   If you use Gmail, you must select this checkbox.
6. In the *Account* field, enter an appropriate administrative email and password. For example, `my-sample-Sitefinity-watch@gmail.com`.
7. If you need to do special mappings, click *Mapping Settings*, and then enter appropriate data in the *New publishing point: Pop3InboundPipe* dialog.
8. Select the preferred *Schedule publication updates interval*.

> info **NOTE:** In the dialog box above, the following message is displayed - *All successfully retrieved mail messages will be deleted from the POP3 server*. Refer to the documentation of your mail server since this is subject to configuration. For example, Gmail allows you to leave a copy of the retrieved messages in your inbox, so that you do not lose any of your emails. For more information, see Gmail POP3 configuration settings.

### Configure how the feed appears on Sitefinity CMS website

In the *Publish as* section, you configure how this information will be displayed on your Sitefinity CMS website. To configure the publishing of the retrieved content, do the following:

1. In *Publish as* section, click *Add more...*
2. Select *Sitefinity content*.
3. In *Import data as* dropdown box select *News items*.
4. Select *Automatically publish imported data*.
5. If you need to do special mappings, click *Mapping Settings*, and then enter appropriate data in the *New publishing point: Pop3InboundPipe* dialog.
6. Click *Done*.
7. Click *Save changes* to save your information and create the feed.

## Create the task

To create a task, perform the following:

1. Open your Sitefinity CMS project in Visual Studio.
2. From the context menu of the project, click *Add » New Item...*
3. In the left pane, select *Visual C# » Code*.
4. Click *Class*.
5. In the *Name* input field, enter `PipeScheduledTask.cs`.
6. Open the newly created file.
7. Add the following code:

   ```C#
   using System;
   using System.Linq;
   using Telerik.Sitefinity.Publishing;
   using Telerik.Sitefinity.Scheduling;

   namespace SitefinityWebApp
   {
       public class PipeScheduledTask : ScheduledTask
       {
           public PipeScheduledTask()
           {
           }

           public override void ExecuteTask()
           {
               PublishingManager pubManager = PublishingManager.GetManager();
               var point = pubManager.GetPublishingPoints().Where((w) => w.Name == "Competitors Newsletters").FirstOrDefault();
               if (point != null)
               {
                   PublishingManager.InvokeInboundPushPipes(point.Id, null);
               }

               SchedulingManager schedulingManager = SchedulingManager.GetManager();

               PipeScheduledTask newTask = new PipeScheduledTask()
               {
                   Key = this.Key,
                   ExecuteTime = DateTime.UtcNow.AddHours(1)
               };

               schedulingManager.AddTask(newTask);
               schedulingManager.SaveChanges();
           }

           public override string TaskName
           {
               get
               {
                   return "SitefinityWebApp.PipeScheduledTask";
               }
           }
       }
   }
   ```

In the code above, `ExecuteTask` method you get the feed from the publishing system and execute it. You must also set the `TaskName` to reflect the full class name so that the system can properly instantiate the task from the factory and execute it at a specified point. Once the task is executed you must invoke the scheduling manager and queue a new task of the same type.

## Schedule the task

To schedule your task as early in the application lifecycle as possible, you must handle the `Initialized` event of the `Bootstrapper` inside the `Application_Start` handler of your application. Perform the following:

1. Open your Sitefinity web app in Visual Studio.
2. From the context menu of the project, click *Add » New Item...*
3. In the left pane, select *Visual C# » Web*.
4. Click *Global Application Class*.
5. Keep the default name `Global.asax`.
6. Open the newly created file.
7. Add the following code:

   ```C#
   using System;
   using System.Linq;
   using Telerik.Sitefinity.Abstractions;
   using Telerik.Sitefinity.Data;
   using Telerik.Sitefinity.Scheduling;

   namespace SitefinityWebApp
   {
       public class Global : System.Web.HttpApplication
       {
           protected void Application_Start(object sender, EventArgs e)
           {
               Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
           }

           private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
           {
               SchedulingManager manager = SchedulingManager.GetManager();
               string myKey = "239CE594-5613-42EC-AC57-8E2B33B28065";

               var count = manager.GetTaskData().Where(i => i.Key == "239CE594-5613-42EC-AC57-8E2B33B28065").ToList().Count;

               if (count == 0)
               {
                   PipeScheduledTask newTask = new PipeScheduledTask()
                   {
                       Key = myKey,
                       ExecuteTime = DateTime.UtcNow.AddSeconds(10),
                   };
                   manager.AddTask(newTask);
                   manager.SaveChanges();
               }
           }
       }
   }
   ```

The `Global.asax` file is used to handle application and session-state events in the ASP.NET website.

You create a key for the task so you can search for it later. The `Key` property of `PipeScheduledTask` is a string. You must set the `ExecuteTime` property of the tasks that determines when this task is going to be executed.

> info **NOTE:** You must set all date and time values in a UTC format in Sitefinity CMS.

Once you queue the tasks and commit the transaction, Sitefinity CMS determines when it must execute the tasks. If it finds a task that is not yet executed and its execution time has already passed, Sitefinity CMS automatically executes and deletes it.
