Blog about tips & tricks for CMS enhancement


License expiration job checker for Episerver sites

Episerver job for notifying

The blog image says it all: your Episerver license has been expired in your client's production environment and is now presented by the traditional warning that Episerver choose to display all over the site.

You've might kept an upcoming event in your scheduled calendar to update the license, but as we all know, the work of a day may turn out to not give a single thought of that issue once it happens.

So that is why you should have a Episerver Schedulued Job for this matter whom emails you when things are about to expire. In that way you may look even more professional to actually tell the customer that his/her license is about to expire, and that he/she should take action for getting a new one.

You may find the code below or at this GitHub-link:
GitHub Gist code

using EPiServer.PlugIn;
using EPiServer.Scheduler;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Xml.Linq;

namespace Episerver_Playground.Business.Episerver_Jobs
        DisplayName = "License Expiration Checker", 
        Description = "Sends an email when your License.config is about to expire." +
        "The \"Scheduled job interval\" will set the frequency of sending the emails in combination with days of interval.",
        SortIndex = 1000)]
    public class LicenseSchedulerChecker : ScheduledJobBase
        #region Properties

        private readonly int _daysOfInterval; 
        private readonly string _domainUrl; 


        public LicenseSchedulerChecker()
            _daysOfInterval = 14; // recommended to set in configuration file
            _domainUrl = ""; // recommended to set in configuration file

        public override string Execute()
            OnStatusChanged(String.Format("Looking for your License..."));

            // Get the license file
            var directory = AppDomain.CurrentDomain.BaseDirectory;
            var licenseFile = Directory.GetFiles(directory, "License.config", SearchOption.AllDirectories).First();

            if (licenseFile == null)
                return "License file couldn't be found!";

            // Read the file
            XDocument file = XDocument.Load(licenseFile);
            var validationDateFromFile = file.Descendants("ValidToRestriction").First();

            // Get and convert date
            var dateValue = validationDateFromFile.FirstAttribute.Value;
            var validDate = DateTime.ParseExact(dateValue, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
            var validForDays = (validDate - DateTime.Now).TotalDays;

            // Check the date
            if (validForDays < _daysOfInterval)
                return SendMail(validForDays, _domainUrl, dateValue);

            return "License file is not in range of expiring.";

        private static string SendMail(double validForDays, string domainUrl, string dateValue)
            var result = string.Format("Mail was sent to {0}", ""); // recommended to set in configuration file

            // SMTP client
            using (var client = new SmtpClient
                UseDefaultCredentials = true
                #region Mail setup
                MailMessage mail = new MailMessage();

                mail.From = new MailAddress(""); // recommended to set in configuration file
                mail.To.Add(new MailAddress("")); // recommended to set in configuration file

                mail.Subject = string.Format("Warning! {0}'s license is about to expire!", domainUrl);
                mail.Body = "You're receiving this email because the Episerver license for <b>" + domainUrl + "</b> is about to expire!" +
                            "<br />" +
                            "Here is the due date and other information:" +
                            "<br />" +
                            "<br />" +
                            "<b>Domain:</b>" + domainUrl + "<br />" +
                            "<b>Project name:</b>" + Assembly.GetCallingAssembly().GetName().Name + "<br />" +
                            "<b>Valid to:</b> " + dateValue + "<br />" +
                            "<br />" +
                            "Not sure if you should receive these emails? Contact your administrator.";
                mail.IsBodyHtml = true;

                catch (Exception ex)
                    result = "Email could not be sent. Check credentials and network settings for your SMTP.";

            return result;

And of course, do not forget to setup the SMTP aswell. You may find the Papercut application as a good choice for testing this in your local development environments as a first approach.

  <smtp deliveryMethod="Network" from="">
    <network host="" port="25" />

Brief summary

Basically the job checks for your License.config file in your root web project and checks the expiration date. Based on your setting to keep days in advance to email the expiration and how often the job is scheduled to run from admin panel in Episerver, you may receive the email throughout a SMTP-server to send along the warning.

The job is tested out and relies on Episerver 10's and above Scheduled job base class but it should work lower CMS versions aswell.

You may set some parameters on your own such as which domain, which project from your solution and the person(s) whom should receive the warning email once the error is triggered.

So from now on no more ignorant license expirations for Episerver sites! Yay!