using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using CMS.EventLog;
using CMS.EmailEngine;
using CMS.GlobalHelper;
using CMS.SiteProvider;
using CMS.SettingsProvider;
using CMS.Notifications;
namespace EmailNotification
{
/// <summary>
/// Base class for e-mail notification gateway.
/// </summary>
public class EmailNotificationGateway : CMSNotificationGateway
{
/// <summary>
/// Returns the e-mail gateway form.
/// </summary>
public override CMSNotificationGatewayForm GetNotificationGatewayForm()
{
try
{
Control ctrl = this.NotificationSubscriptionControl.Page.LoadControl("~/CMSModules/Notifications/Controls/NotificationSubscription/EmailNotificationForm.ascx");
ctrl.ID = ValidationHelper.GetIdentificator("notif" + this.NotificationGatewayObj.GatewayName);
return (CMSNotificationGatewayForm)ctrl;
}
catch (Exception ex)
{
try
{
// Log the event
EventLogProvider eventLog = new EventLogProvider();
eventLog.LogEvent("EmailGateway", "EXCEPTION", ex);
}
catch
{
// Unable to log the event
}
}
return null;
}
/// <summary>
/// Sends the notification via e-mail.
/// </summary>
public override void SendNotification()
{
try
{
if (this.NotificationSubscriptionObj != null)
{
// Get template text
NotificationTemplateTextInfo templateText = NotificationTemplateTextInfoProvider.GetNotificationTemplateTextInfo(this.NotificationGatewayObj.GatewayID, this.NotificationSubscriptionObj.SubscriptionTemplateID);
if (templateText != null)
{
// Get the site name
string siteName = null;
SiteInfo si = SiteInfoProvider.GetSiteInfo(this.NotificationSubscriptionObj.SubscriptionSiteID);
if (si != null)
{
siteName = si.SiteName;
}
// Create message object
EmailMessage message = new EmailMessage();
// Get sender from settings
message.From = SettingsKeyProvider.GetStringValue(siteName + ".CMSSendEmailNotificationsFrom");
// Do not send the e-mail if there is no sender specified
if (message.From != "")
{
// Initialize message
message.Recipients = this.NotificationSubscriptionObj.SubscriptionTarget;
// Body
if ((this.NotificationSubscriptionObj.SubscriptionUseHTML) &&
(this.NotificationGatewayObj.GatewaySupportsHTMLText) &&
(templateText.TemplateHTMLText != ""))
{
// HTML format, set Body property, resolve macros if possible
message.EmailFormat = EmailFormatEnum.Html;
if (this.Resolver != null)
{
message.Body = this.Resolver.ResolveMacros(templateText.TemplateHTMLText);
}
else
{
message.Body = templateText.TemplateHTMLText;
}
}
else
{
// Plaintext format, set PlainTextBody property, resolve macros if possible
message.EmailFormat = EmailFormatEnum.PlainText;
if (this.Resolver != null)
{
message.PlainTextBody = this.Resolver.ResolveMacros(templateText.TemplatePlainText);
}
else
{
message.PlainTextBody = templateText.TemplatePlainText;
}
}
// Subject, resolve macros if possible
if (this.Resolver != null)
{
message.Subject = this.Resolver.ResolveMacros(templateText.TemplateSubject);
}
else
{
message.Subject = templateText.TemplateSubject;
}
// Send email via Email engine API
EmailSender.SendEmail(siteName, message);
}
}
}
}
catch (Exception ex)
{
try
{
// Log the event
EventLogProvider eventLog = new EventLogProvider();
eventLog.LogEvent("EmailGateway", "EXCEPTION", ex);
}
catch
{
// Unable to log the event
}
}
}
}
}
|