Below is an example of .Net core 3.1 Console App that is reading a mailbox on Exchange and checking the count of mails. If the mail count cross a certain threshold, it’ll check for an e-mail older than 2 hours to send an alert.
Such an example can be used to schedule alerts or log information if you’re tracking mails in a mailbox.
Install Nuget package Microsoft.Exchange.WebServices to use ExchangeService class.
public static void Main(string args)
string Subject = "";
string sUsername = ConfigurationManager.AppSettings.Get("UserName");
var section = ConfigurationManager.GetSection("secureAppSettings") as NameValueCollection;
string vCount = ConfigurationManager.AppSettings.Get("Count");
int Count = Convert.ToInt32(vCount);
string sPassword = ConfigurationManager.AppSettings.Get("Password");
string sDomain = ConfigurationManager.AppSettings.Get("Domain");
TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
DateTime comparedate = indianTime.AddHours(-2);
DateTime MailDateTime = comparedate;
ExchangeService exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
exchange.Credentials = new WebCredentials(sUsername, sPassword, sDomain);
exchange.Url = new Uri(ConfigurationManager.AppSettings.Get("ExchangeUrl"));
Folder inbox = Folder.Bind(exchange, WellKnownFolderName.Inbox);
var oSendemail = 0;
TextlogMessage = "Start With count:" + inbox.TotalCount;
if (inbox.TotalCount > Count)
Console.WriteLine("Count " + inbox.TotalCount);
ItemView view = new ItemView(1);
var findResults = exchange.FindItems(WellKnownFolderName.Inbox, view);
foreach (Item item in findResults.Items)
Console.WriteLine("Test: " + item.DateTimeReceived + " %" + comparedate);
DateTime newdate = TimeZoneInfo.ConvertTimeToUtc(item.DateTimeReceived);
int Dresult = DateTime.Compare(newdate, comparedate);
if (Dresult < 0)
Console.WriteLine("Alert " + "Sub" + item.Subject + newdate + " %" + comparedate);
oSendemail = 1;
MailDateTime = newdate;
Subject = item.Subject;
if (oSendemail > 0)
Program p = new Program();
//To Do Send Email Logic..
p.SendEmail(exchange, oSendemail, inbox.TotalCount, TextlogMessage);
TextlogMessage = TextlogMessage + " EmailTime: " + MailDateTime + "CompareTime " + comparedate + " Alert Sent Subject : " + Subject;
//To Do logging..
I have an Intranet Application where a .docm template downloaded from a ReactJS UI as a blob is opening in Protected View on the users’ machines. This is happening when downloading in browsers other than IE or Edge browser in IE mode.
When you download the document from Chrome or other browsers, MS Word is considering it as an Internet Application. Hence, the checkbox for Protected View is applying these changes and downloading it in Protected View.
The code to download this as a blob is in ReactJS is available here.
You’ll need to open Command Prompt in Admin mode to make the changes especially when User Account Control (UAC) is enabled on your Windows 10 machine. CD to system32 for 64-bit registration and SysWoW64 for 32-bit registration. Place the COM component in the folder from you want to register and also your executable is placed e.g. “C:\TFS\Code\bin\” in the example below.
To register use the below command:
To unregister use the below command:
regsvr32 /u "C:\TFS\Code\bin\MailObj.dll"
You should get a prompt that the register/unregister command succeeded. This command can be used to register/unregister DLL or OCX files.
Robocopy is a robust file copy command for the Windows command line. It allows users to copy files, directories, and even drives from one location to another. It can be automated with Task Scheduler to run at specific times.
Robocopy example to copy a file from Source to Destination:
Robocopy [Src] [Dest] [filename] [options]
Src: Can be local or UNC path
Dest: Can be local or UNC path
Code Sample batch file:
echo Sync Started
Robocopy C:\Data\TestFolder C:\Data\Logs\TestFolder\%DTVARYEAR%-%DTVARMONTH%-%DTVARDATE%_TestFolder testlogfile.log /FFT /Z /XA:H /LOG+:C:\Data\SchedTasks\LogsTest\%DTVARYEAR%-%DTVARMONTH%_AppLogs.txt
echo Log synced
The above code will copy the testlogfile.log from Source to Destination.
Options: /FFT Assumes FAT file times (two-second precision). /Z Copies files in restartable mode. /XA:H exclude hidden file /LOG+ log output to file and append.
Additional useful options: /MIR Mirrors a directory tree (equivalent to /e plus /purge). /XF exclude specified file /XD exclude specified directory
Suppose you need to hide the password in your clear text connection strings at a particular back-up path or any other path in config files. This can be achieved using PowerShell with the following code.
Today, one of the users of my application wanted to know if we can find out the original title of a ticket created in the Application. Although, we do not store any such logs for modifications, while on the verge of giving up, I remembered we archive the e-mails received by the Windows Service application that processes the e-mails and archives them at a location on the Server on which it is installed. The e-mail Subject line is stored as the ticket title. These tickets which are created by the Windows Service can be viewed in the Web Application.
Since the folder with the archiving e-mails have a lot of ’em, simply doing a Windows Search is a time killer! So I remembered my way through the good old DOS commands.
Since the .eml file has the ticket number, I used the dir command to search for the e-mail name and copy command to copy it to another location quickly.
Firstly cd to the location where the archived e-mails are stored on the Server:
So e.g. if the ticket number is 123456
dir *123456* /s
The /s searches for the File name inside the two wildcard characters asterisk “*” with the dir command.
Then if the file name is e.g. TICKET123456 – abc.eml then use copy command as follows within the same directory:
copy "TICKET123456 - abc.eml" c:\Temp
So, with the e-mail having the original Subject Line preserved, I could share the original ticket title.