Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
{
[ScheduledPlugIn(
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.",
GUID = "{90B9C2F5-E1B1-4A34-B9FC-37AA4EC8C16F}",
SortIndex = 1000)]
public class LicenseSchedulerChecker : ScheduledJobBase
{
#region Properties
private readonly int _daysOfInterval;
private readonly string _domainUrl;
#endregion
public LicenseSchedulerChecker()
{
_daysOfInterval = 14; // recommended to set in configuration file
_domainUrl = "http://consid.se"; // 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}", "info@consid.se"); // 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("info@consid.se"); // recommended to set in configuration file
mail.To.Add(new MailAddress("receiveraddress@hotmail.com")); // 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;
#endregion
try
{
client.Send(mail);
}
catch (Exception ex)
{
result = "Email could not be sent. Check credentials and network settings for your SMTP.";
}
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment