Email Notifications System

/// <summary>
/// Notifications system
/// </summary>
/// <returns>Result object</returns>
private Result NotificationsSystem()
{
    // Auxiliary variables
    DataTable _dtRecords;
    Result _Result = new Result();

    // Get results from database
    _dtRecords = GetResults();

    // Check success
    if (_dtRecords != null && _dtRecords.Rows.Count > 0)
    {
        // Load email template file path
        string _strEmailTemplate = LoadEmailTemplate();

        // Check email template path is not null
        if (string.IsNullOrEmpty(_strEmailTemplate))
        {
            // Add error message
            _Result.AddError("Undefined email template!");

            // Return
            return _Result;
        }

        // Check if template file exists
        if (!File.Exists(_strEmailTemplate))
        {
            // Add error message
            _Result.AddError("Email template doesn't exist!");

            // Return
            return _Result;
        }

        // Load excel directory path
        string _strExcelFilesPath = LoadExcelFilesPath();

        // Check if directory exists
        if (!Directory.Exists(Path.GetDirectoryName(_strExcelFilesPath)))
        {
            // Create directory
            Directory.CreateDirectory(Path.GetDirectoryName(_strExcelFilesPath));
        }

        // Load recipient emails list
        List<string> _lstRecipientEmails = LoadRecipientEmailsList();

        // Check if list are undefined
        if (_lstRecipientEmails == null || !_lstRecipientEmails.Any())
        {
            // Add error message
            _Result.AddError("Undefined recipient!");

            // Return
            return _Result;
        }

        // Generate excel file name
        string _strExcelFileName = Path.Combine(_strExcelFilesPath, string.Concat(Path.GetFileNameWithoutExtension(_strEmailTemplate), "_", Guid.NewGuid().ToString(), ".xlsx"));

        // Create excel file
        bool _blSuccess = ClExcel.CreateExcelFileFromDataTable(_dtRecords, _strExcelFileName);

        // Check success
        if (!_blSuccess)
        {
            // Add error message
            _Result.AddError("Error while creating the Excel file!");

            // Return
            return _Result;
        }

        // Send email
        SendEmail(_lstRecipientEmails, _strEmailTemplate, _strExcelFileName);
    }
    else
    {
        // Add info message
        _Result.AddInfo("No data is available!");

        // Return
        return _Result;
    }

    // Return
    return _Result;
}

/// <summary>
/// Send email
/// </summary>
/// <param name="_lstRecipientEmails">Recipient emails list</param>
/// <param name="_strEmailTemplate">Email template</param>
/// <param name="_strExcelFileName">Excel file name</param>
void SendEmail(List<string> _lstRecipientEmails, string _strEmailTemplate, string _strExcelFileName)
{
    // Email configurations
    string _strFrom = "FromEmail@gmail.com";
    string _strFromPassword = "FromPassword";

    // Create email message
    MailMessage _Message = new MailMessage();

    // Set from address
    _Message.From = new MailAddress(_strFrom);

    // Set subject
    _Message.Subject = "Subject Message!";

    // Set recipient list
    foreach (string item in _lstRecipientEmails)
        _Message.To.Add(new MailAddress(item));

    // Set body message (email template file
    _Message.Body = File.ReadAllText(_strEmailTemplate, Encoding.Default);
    _Message.IsBodyHtml = true;

    // Set attachment (Excel file)
    _Message.Attachments.Add(new Attachment(_strExcelFileName));

    // STMP configurations
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com")
    {
        Port = 587,
        Credentials = new System.Net.NetworkCredential(_strFrom, _strFromPassword),
        EnableSsl = true
    };

    // Send email
    smtpClient.Send(_Message);
}

/// <summary>
/// Get results from database
/// </summary>
/// <returns>Data table with the query results</returns>
DataTable GetResults()
{
    // Getting data from database should be implemented
}

/// <summary>
/// Load email template
/// </summary>
/// <returns>String with the email template path</returns>
string LoadEmailTemplate()
{
    // Getting the configuration of the email template must be implemented (.html file)
}

/// <summary>
/// Load Excel files path
/// </summary>
/// <returns>String with the Excel files directory</returns>
string LoadExcelFilesPath()
{
    // Getting the configuration of Excel files path must be implemented
}

/// <summary>
/// Load recipient emails list
/// </summary>
/// <returns>List of recipient emails list</returns>
List<string> LoadRecipientEmailsList()
{
    // Getting the recipient emails list must be implemented
}