The CloudMailIn library contains the interfaces required for adding methods to the generic CloudMailIn API Handler.
A class library should be built as per the below specifications and copied into the same folder as the cloudmailin application. On application startup all libraries named WFD.CloudMailIn.Library.*.dll will be loaded. When a request hits the API the ProcessEmail method will be ran from all loaded libraries against the email received.
To implement the library simply copy the DLL into the bin folder of the required CloudMailIn API IIS install.
`IMPORTANT! Libraries will only be executed if the name of them follows the naming convention of WFD.CloudMailIn.Library.*.dll
Usage
Any class that inherits from the IEmailInProcessor interface will be executed by the generic API. public className : EmailInProcessor, IEmailInProcessor { public className() { //When initialising a profile the settings should always be loaded in first this.LoadSettings();
//Set the target NLog profile, see logging section for more details
this.NLogTarget = "Main";
}
public String Description { get { //Return a simple description of what the profile does return "This is a default description"; } }
//Used by the processor to determine status public Boolean IsValid { get; set; }
public String Name { get { //Return the name of the profile return "Email Attachment Saver"; } }
//All specific profile settings are held in a dictionary public Dictionary Settings { get; set; }
public void LoadSettings() { //Add code to load the settings from the saved profile /*Sample code String configFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, String.Format("{0}.wfd.config", Assembly.GetExecutingAssembly().GetName().Name));
this.Settings = new Dictionary();
//First run of profile, create all config settings
if (File.Exists(configFileName) == false)
{
this.Settings.Add("SaveLocation", "C:\\WFD");
using (FileStream stream = new FileStream(configFileName, FileMode.CreateNew))
{
stream.Write(new byte[0], 0, 0);
stream.Close();
}
//Run initial save to ensure data is in file
this.SaveSettings();
}
else
{
String[] fileLines = File.ReadAllLines(configFileName);
foreach (String line in fileLines)
{
String[] data = line.Split('|');
this.Settings.Add(data[0], data[1]);
}
}*/
}
public async Task> ProcessEmail(EmailInDTO email) { //The code to be run by the API when an email is received }
public void SaveSettings() { //Handle saving of the profile settings /* Sample code String configFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, String.Format("{0}.wfd.config", Assembly.GetExecutingAssembly().GetName().Name));
File.WriteAllText(configFileName, "");
foreach (KeyValuePair setting in this.Settings)
{
File.AppendAllText(configFileName, String.Format("{0}|{1}{2}", setting.Key, setting.Value, Environment.NewLine));
}*/
}
public async Task Validate() { //Validate the profile itself is correct (file paths etc) }
public async Task ValidateEmailInObject(EmailInDTO emailIn) { //Add code to validate the EmailIn object before processing begins } }
Logging
Any class that inherits from the EmailInProcessor base class
will also have access to a Logger property. You can use this
property to write Trace, Information & Errors logs
By default the Logger object uses the 'Main' target from the NLog.config file
If this needs to be changed override the NlogTarget property
public ClassName : EmailInProcessor, IEmailInProcessor
{
public ClassName() : base()
{
this.NLogTarget = "CustomLogger";
}
public async Task> ProcessEmail(EmailInDTO email)
{
try
{
}
catch (Exception ex)
{
this.Logger.Error(ex.Message);
}
}
}