C#

Using Response.Write in a C# Class

By on April 22, 2021

It’s not that difficult to use Response.Write() in a .NET Framework WebForms class.
(more…)

Using Microsoft Office MIME Types with MailKit

By on October 21, 2019

Recently I had problems with attaching a macro-enabled Excel workbook to an email. When it was received, the macro’s didn’t run correctly on the latest version of Microsoft Office. It did run correctly on Excel 2007. The workbook was being corrupted in the encoding into the email or decoding when its downloaded.

The following code will maintain the integrity of the Excel workbook when attached to an e-mail:

ContentType xlsmContent = new ContentType ("application/vnd.ms-excel.sheet.macroEnabled.12", "octet-steam");
builder.Attachments.Add ($"{Directory.GetCurrentDirectory ()}/Temp/workbook.xlsm", xlsmContent);

These are the MIME types for documents in Microsoft Office:

Ext MIME Type
.doc application/msword
.dot application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm application/vnd.ms-word.document.macroEnabled.12
.dotm application/vnd.ms-word.template.macroEnabled.12
.xls application/vnd.ms-excel
.xlt application/vnd.ms-excel
.xla application/vnd.ms-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm application/vnd.ms-excel.sheet.macroEnabled.12
.xltm application/vnd.ms-excel.template.macroEnabled.12
.xlam application/vnd.ms-excel.addin.macroEnabled.12
.xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt application/vnd.ms-powerpoint
.pot application/vnd.ms-powerpoint
.pps application/vnd.ms-powerpoint
.ppa application/vnd.ms-powerpoint
.pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm application/vnd.ms-powerpoint.presentation.macroEnabled.12
.ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12

C#: Hello World!

By on May 16, 2017

This is the “Hello World! program that every programmer learns in their first 5 minutes of their schooling. Compile this as a console project to test if your compiler is working.

ConsoleApplication1
{
   class Program
   {
      static void Main (string [] args)
      {
         Console.WriteLine ("Hello World!");
         Console.ReadKey ();
      }
   }
}