M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Sending Emails

This tutorial explains how to implement email sending functionality using M# framework. We will learn about the configuration required and the Interface provided in M# framework.

IEmailQueueItem Interface

M# Framework exposes this interface under "Msharp.Framework.Services" namespace, which enables you to implement email sending functionality. In order to send an email in your website using M# framework, you must have an entity which implements this interface and you must have a database table having columns as per the properties in this interface. The screenshot below shows the definition of IEmailQueueItem interface:

Implementing Interface

In order to implement the interface we have created a new entity in our "HelloWord" project, which has all the properties defined in the IEmailQueueItem interface (For more information on creating entities in M#, please read tutorial Entity, Page, Module).

Note: The screenshots above shows EmailQueueItem entity created and a database table generated by M#, marked as soft delete, because we want to keep the log of what we will be sending in emails (Database.Delete tutorial explains soft deleting in more detail).

The screenshot above demonstrates the partial logic class of our EmailQueueItem entity, which implements the IEmailQueueItem interface (For details on creating business logic please read tutorial Partial Classes and Business Logic).

Email Configuration

As we have successfully implemented IEmailQueueItem interface, now we need to look at some configuration in our web.config file. The configuration required to send an email is generated by M# when a project is created, but we will take a look at these settings to know which settings are used for what purpose.

The "App Settings" shown below are generated by M# in web.config file to support the email sending functionality

web.config "Mail Settings" section contains the information used in M# framework to read the SMTP settings.

Sending an Email

The screenshots and explanation above was all about setting up IEmailQueueItem interface and configuration required. Now to send an email in M# we do not have to do anything complex. Simply populate the EmailQueueItem instance with required details and call "Database.Save()" method to save the instance in database.

The screenshot above demonstrates that we have implemented a new method in our Employee class, which populates a new instance of EmailQueueItem and simply saves it in database. This method is called in "OnSaved" method of Employee class, which is raised just after a new employee is saved in database (For information on saving data and entity events, please read tutorial Database.Save in chapter 4).

M# automatic process "SendEmailQueueItems" then sends this newly inserted EmailQueueItem record using EmailService class (For more details on SendEmailQueueItems process, please read tutorial Automatic Tasks in chapter 13).

Email Service Class

EmailService is a static class provided under "MSharp.Framework.Services" namespace. This class encapsulates the functionality to send an email using the configuration explained above in this tutorial. This class exposes methods to send emails by requiring "IEmailQueueItem" type argument. The screenshots below show the declaration of the methods to send an email.

Note: "SendAll" method overloads retrieve email records from "EmailQueueItem" database table.

Although SMTP setting are read from Web.Config EmailService class also implements a method to override SMTP settings configuration, as shown below:

We often need to perform custom action before, or just after sending an email and also when an error occurs while sending an email. EmailService raises events for each action, which can be wired to perform custom logic. Shown below are the events and their description