Skip to content

Commit

Permalink
Adding the email support that was coded at the NDC 2012 Hackathon.
Browse files Browse the repository at this point in the history
Closes #446
  • Loading branch information
andreasohlund committed Jun 7, 2012
1 parent f798f46 commit 0bb5516
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Samples/Notifications/Notifications.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NotifySample", "NotifySample\NotifySample.csproj", "{58400EA4-C837-434E-B5AC-7540287F7BDE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{58400EA4-C837-434E-B5AC-7540287F7BDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58400EA4-C837-434E-B5AC-7540287F7BDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58400EA4-C837-434E-B5AC-7540287F7BDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58400EA4-C837-434E-B5AC-7540287F7BDE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
22 changes: 22 additions & 0 deletions Samples/Notifications/NotifySample/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MessageForwardingInCaseOfFaultConfig"
type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
</configSections>

<system.net>
<mailSettings>
<smtp from="X" deliveryMethod="Network">
<network defaultCredentials="false"
host="smtp.gmail.com"
port="587"
userName="username"
password="pwd"
enableSsl="true" />
</smtp>
</mailSettings>
</system.net>

<MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />
</configuration>
24 changes: 24 additions & 0 deletions Samples/Notifications/NotifySample/EmailSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace NotifySample
{
using System;
using System.Net.Mail;
using NServiceBus;

public class EmailSender : IWantToRunAtStartup
{
public IBus Bus { get; set; }

public void Run()
{
Console.WriteLine("Hit any key to send a email using the notification satellite");

while (Console.ReadKey().Key.ToString().ToLower() != "q")
Bus.SendEmail(new MailMessage("X", "Y", "Hello from the the NSB notification support", "Tha body"));
}

public void Stop()
{
//no-op
}
}
}
8 changes: 8 additions & 0 deletions Samples/Notifications/NotifySample/EndpointConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NotifySample
{
using NServiceBus;

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{
}
}
65 changes: 65 additions & 0 deletions Samples/Notifications/NotifySample/NotifySample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{58400EA4-C837-434E-B5AC-7540287F7BDE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NotifySample</RootNamespace>
<AssemblyName>NotifySample</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net">
<HintPath>..\..\..\binaries\log4net.dll</HintPath>
</Reference>
<Reference Include="NServiceBus">
<HintPath>..\..\..\binaries\NServiceBus.dll</HintPath>
</Reference>
<Reference Include="NServiceBus.Core">
<HintPath>..\..\..\binaries\NServiceBus.Core.dll</HintPath>
</Reference>
<Reference Include="NServiceBus.Host">
<HintPath>..\..\..\binaries\NServiceBus.Host.exe</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="EmailSender.cs" />
<Compile Include="EndpointConfig.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<StartAction>Program</StartAction>
<StartProgram>$(ProjectDir)$(OutputPath)NServiceBus.Host.exe</StartProgram>
</PropertyGroup>
</Project>
36 changes: 36 additions & 0 deletions Samples/Notifications/NotifySample/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("NotifySample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NotifySample")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("acf83a72-5535-4a5d-adcc-453aa7c23cf8")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
2 changes: 1 addition & 1 deletion default.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ task TestMain -depends CompileMain -description "Builds NServiceBus.dll, keeps t

task CompileCore -depends InitEnvironment -description "Builds NServiceBus.Core.dll and keeps the output in \binaries" {

$coreDirs = "unicastTransport", "ObjectBuilder", "config", "faults", "utils", "messageInterfaces", "impl\messageInterfaces", "config", "logging", "Impl\ObjectBuilder.Common", "installation", "encryption", "unitofwork", "masterNode", "impl\installation", "impl\unicast\NServiceBus.Unicast.Msmq", "impl\Serializers", "impl\licensing", "unicast", "headers", "impersonation", "impl\unicast\queuing", "impl\unicast\transport", "impl\unicast\NServiceBus.Unicast.Subscriptions.Msmq", "impl\unicast\NServiceBus.Unicast.Subscriptions.InMemory", "impl\faults", "impl\encryption", "databus", "impl\Sagas", "impl\SagaPersisters\InMemory", "impl\SagaPersisters\RavenSagaPersister", "impl\unicast\NServiceBus.Unicast.Subscriptions.Raven", "integration", "impl\databus", "distributor", "gateway", "scheduling", "satellites", "management\retries", "timeout"
$coreDirs = "unicastTransport", "ObjectBuilder", "config", "faults", "utils", "messageInterfaces", "impl\messageInterfaces", "config", "logging", "Impl\ObjectBuilder.Common", "installation", "encryption", "unitofwork", "masterNode", "impl\installation", "impl\unicast\NServiceBus.Unicast.Msmq", "impl\Serializers", "impl\licensing", "unicast", "headers", "impersonation", "impl\unicast\queuing", "impl\unicast\transport", "impl\unicast\NServiceBus.Unicast.Subscriptions.Msmq", "impl\unicast\NServiceBus.Unicast.Subscriptions.InMemory", "impl\faults", "impl\encryption", "databus", "impl\Sagas", "impl\SagaPersisters\InMemory", "impl\SagaPersisters\RavenSagaPersister", "impl\unicast\NServiceBus.Unicast.Subscriptions.Raven", "integration", "impl\databus", "distributor", "gateway", "scheduling", "satellites", "management\retries", "timeout", "Notifications"
$coreDirs | % {
$solutionDir = Resolve-Path "$srcDir\$_"
cd $solutionDir
Expand Down
23 changes: 23 additions & 0 deletions src/Notifications/NServiceBus.Notifications/BusExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace NServiceBus
{
using System;
using System.Net.Mail;
using Config;
using Notifications;

public static class BusExtensions
{
public static void SendEmail(this IBus bus,MailMessage message)
{
if (ConfigureNotifications.NotificationsDisabled)
throw new InvalidOperationException("Send email is not supported if notifications is disabled. Please remove Configure.DisableNotifications() from your config.");

bus.Send(NotificationAddess, new SendEmail
{
Message = message
});
}

public static Address NotificationAddess = Address.Local.SubScope("Notifications");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace NServiceBus.Config
{
public static class ConfigureNotifications
{
public static Configure DisableNotifications(this Configure config)
{
NotificationsDisabled = true;
return config;
}

public static bool NotificationsDisabled { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{D7813FA0-5E4B-477A-BD49-791782249B38}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NServiceBus.Notifications</RootNamespace>
<AssemblyName>NServiceBus.Notifications</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="NServiceBus">
<HintPath>..\..\..\build\output\NServiceBus.dll</HintPath>
</Reference>
<Reference Include="NServiceBus.Config">
<HintPath>..\..\..\build\nservicebus.core\NServiceBus.Config.dll</HintPath>
</Reference>
<Reference Include="NServiceBus.Installation">
<HintPath>..\..\..\build\nservicebus.core\NServiceBus.Installation.dll</HintPath>
</Reference>
<Reference Include="NServiceBus.Installation.Windows">
<HintPath>..\..\..\build\nservicebus.core\NServiceBus.Installation.Windows.dll</HintPath>
</Reference>
<Reference Include="NServiceBus.Satellites">
<HintPath>..\..\..\build\nservicebus.core\NServiceBus.Satellites.dll</HintPath>
</Reference>
<Reference Include="NServiceBus.Utils">
<HintPath>..\..\..\build\nservicebus.core\NServiceBus.Utils.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="Config\ConfigureNotifications.cs" />
<Compile Include="RegisterSystemMessages.cs" />
<Compile Include="BusExtensions.cs" />
<Compile Include="SendEmail.cs" />
<Compile Include="NotificationSatellite.cs" />
<Compile Include="NotifierQueueInstaller.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace NServiceBus.Notifications
{
using System.IO;
using System.Linq;
using System.Net.Mail;
using Config;
using NServiceBus;
using Satellites;
using Serialization;

public class NotificationSatellite : ISatellite
{
public IMessageSerializer MessageSerializer { get; set; }

public void Handle(TransportMessage message)
{
SendEmail sendEmail;

using (var stream = new MemoryStream(message.Body))
sendEmail = (SendEmail)MessageSerializer.Deserialize(stream).First();

using (var c = new SmtpClient())
c.Send(sendEmail.Message);
}

public void Start()
{
//no-op
}

public void Stop()
{
//no-op
}

public Address InputAddress
{
get
{
return BusExtensions.NotificationAddess;
}
}

public bool Disabled
{
get { return ConfigureNotifications.NotificationsDisabled; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace NServiceBus.Notifications
{
using System.Security.Principal;
using Config;
using Installation;
using Installation.Environments;

public class NotifierQueueInstaller:INeedToInstallSomething<Windows>
{
public void Install(WindowsIdentity identity)
{
if (ConfigureNotifications.NotificationsDisabled)
return;

Utils.MsmqUtilities.CreateQueueIfNecessary(BusExtensions.NotificationAddess,identity.Name);
}
}
}
Loading

7 comments on commit 0bb5516

@johannesg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in the NServiceBus namespace? Can the SendEmail extension be moved to another namespace, like NServiceBus.Notifications?
And, instead of bloating the IBus interface, how about an extra property on the bus, like Bus.Notifications.SendEmail(). This is how RavenDB solves it with it's "Advanced" section of the client.

@udidahan
Copy link
Member

@udidahan udidahan commented on 0bb5516 Jun 8, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johannesg
Copy link
Contributor

@johannesg johannesg commented on 0bb5516 Jun 8, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NServiceBus
Copy link

@NServiceBus NServiceBus commented on 0bb5516 Jun 8, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NTTAKR
Copy link

@NTTAKR NTTAKR commented on 0bb5516 Jun 20, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question about the SMTP mail sending addon:

Delivering the mail to your own mailserver should work in most cases, but does not tell anything about success or failure to deliver the mail to its destination.

Would you consider an option to look up the destination mailserver via DNS (MX records) and deliver directly to any of the mailservers known there? This would - of course - mean more work on the delivery process, since you cannot use the .NET framework built in SMTP classes, but instead have to follow the SMTP protocol and deliver yourself.
But it gives you much more information about if the mail could be delivered or not. E.g. "recipient not known" or "mailbox full" are errors which are sent back by the destination mailserver and you could react on this messages.

@ramonsmits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? Will this be some kind of SendLocal containing a serialized version of the passed MailMessage? If so, does it get the same (2nd leve) retry mechanism as all other messages?

@SimonCropp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ramonsmits best to read through this issue #1052

Please sign in to comment.