Created
July 2, 2014 22:53
-
-
Save agross/a5492983eaa3b4f219a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using System.Net.Mail; | |
using System.Net.Mime; | |
using System.Text; | |
using Castle.Core.Smtp; | |
using NServiceBus; | |
using Ops.Contracts.Commands; | |
using System.Linq; | |
namespace Ops | |
{ | |
public class SendEmailHandler : IHandleMessages<SendEmail> | |
{ | |
readonly IEmailSender _emailSender; | |
public SendEmailHandler(IEmailSender emailSender) | |
{ | |
_emailSender = emailSender; | |
} | |
public string SmtpFromAddress { get; set; } | |
public void Handle(SendeEmail message) | |
{ | |
var mail = new MailMessage("it@will-be-overwritten", message.Recipient) | |
{ | |
From = new MailAddress(SmtpFromAddress, "blah"), | |
Subject = message.Betreff, | |
SubjectEncoding = Encoding.UTF8, | |
Body = message.Text ?? message.Html, | |
BodyEncoding = Encoding.UTF8, | |
IsBodyHtml = message.Text == null | |
}; | |
if (message.Html != null && message.Text != null) | |
{ | |
mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(message.Html, | |
Encoding.UTF8, | |
MediaTypeNames.Text.Html)); | |
} | |
var streams = new List<MemoryStream>(); | |
try | |
{ | |
if (message.Attachments != null) | |
{ | |
message.Attachments.ToList().ForEach(info => | |
{ | |
var stream = new MemoryStream(info.Content); | |
streams.Add(stream); | |
mail.Attachments.Add(new Attachment(stream, info.FileName, info.MediaType)); | |
}); | |
} | |
_emailSender.Send(mail); | |
} | |
finally | |
{ | |
streams.ForEach(stream => stream.Dispose()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment