Skip to content

Instantly share code, notes, and snippets.

@Boggin
Created October 15, 2013 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Boggin/6993401 to your computer and use it in GitHub Desktop.
Save Boggin/6993401 to your computer and use it in GitHub Desktop.
Notification service with email as an example.
namespace Demo.Notification
{
using System.Net.Mail;
public class EmailService : IEmailService
{
public void Notify(NotificationDto notification)
{
var msg = new MailMessage
{
Body = notification.Message,
From = new MailAddress(notification.ReplyTo),
IsBodyHtml = notification.IsHtml,
Subject = notification.Title
};
foreach (string addressee in notification.Addressees)
{
msg.Bcc.Add(addressee);
}
using (var smtpClient = new SmtpClient { Host = "localhost" })
{
// Use Papercut [papercut.codeplex.com] for test server.
smtpClient.Send(msg);
}
msg.Dispose();
}
}
}
namespace Demo.Business.DTOs
{
public class NotificationDto
{
/// <summary>
/// Gets or sets multiple recipients' addresses.
/// </summary>
/// <remarks>
/// These could be email addresses or mobile phone numbers or
/// whatever fits with the <c>NotificationMedium</c>.
/// </remarks>
public string[] Addressees { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the message is marked up with HTML.
/// </summary>
public bool IsHtml { get; set; }
public string Message { get; set; }
public string ReplyTo { get; set; }
public string Title { get; set; }
}
}
namespace Demo.Notification
{
public class NotificationService : INotificationService
{
public NotificationService(IEmailService emailService)
{
this.EmailService = emailService;
}
protected IEmailService EmailService { get; set; }
public void NotifyIssueSaved(int issueId, string issueName, IEnumerable<PersonDto> users)
{
if (!this.Configuration.Enabled)
{
return;
}
var addresses = users.Select(user => user.Mail).ToArray();
// create a notification.
var notificationDto = new NotificationDto
{
Message = "Updated.",
ReplyTo = "no-reply@demo.co.uk",
Addressees = addresses,
Title = "Update"
};
// create a task (TPL) for each type of notification that the user's interested in.
Task.Factory.StartNew(() => this.EmailService.Notify(notificationDto));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment