Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit 6793b36

Browse files
Feature/add notification from core (#1)
* Add support for Core.Notification (#72) * Change gitversion (#72) * Fix the gitversion * Remove Core naming from project (#72) * More changes after removing Core in namespace * Remove extension & circular ref issue * Remove project * Update README * Fix the NuGet attributes to point to the correct repository * C# version change * Format change * Review comment Co-authored-by: Mohammad Moattar <[email protected]>
1 parent e0c1415 commit 6793b36

File tree

47 files changed

+1773
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1773
-17
lines changed

README.md

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,81 @@
1-
Library Name
2-
============
1+
# Easify Extensions - Notification Services
32

3+
The project consist of several packages which facilitate sending notifications. It covers the following scenarios:
44
![Release](https://github.com/icgam/Easify.Extensions.Notifications/workflows/Release%20build%20on%20master/main/badge.svg) ![CI](https://github.com/icgam/Easify.Extensions.Notifications/workflows/CI%20on%20Branches%20and%20PRs/badge.svg) ![](https://img.shields.io/nuget/v/Easify.Extensions.Notifications.Extensions.svg?style=flat-square)
55

6-
Brief description of the component or library.
6+
1. Rendering the output message content (Current support is for handle bars using https://github.com/Antaris/FuManchu library)
77

8-
## Get Started
8+
2. Sending messages to multiple audiences using Smtp protocol (It has been implemented using https://github.com/jstedfast/MailKit)
99

10-
Description of how to start working with library
10+
## Usage
1111

12-
### How to use
12+
### Installation
1313

14-
The usage of the library or link to the relevant wiki page need to be here.
14+
Using NuGet
1515

16-
### How to Engage, Contribute, and Give Feedback
16+
```
17+
Install-Package Easify.Extensions.Notifications
18+
```
1719

18-
Description of the steps or process to be a contributor to the project.
20+
Using dotnet cli
1921

20-
Some of the best ways to contribute are to try things out, file issues, join in design conversations,
21-
and make pull-requests.
22+
```
23+
dotnet add package Easify.Extensions.Notifications
24+
```
2225

23-
* [Be an active contributor](./docs/CONTRIBUTING.md): Check out the contributing page to see the best places to log issues and start discussions.
24-
* [Roadmap](./docs/ROADMAP.md): The schedule and milestone themes for project.
26+
### Configuration
2527

26-
## Reporting bugs and useful features
28+
There is an extension which help to setup the service for IServiceCollection.
2729

28-
Security issues and bugs should be reported by creating the relevant features and bugs in issues sections
30+
```csharp
31+
services.AddNotification(configuration)
32+
```
2933

30-
## Related projects
34+
which is adding the following services to the DI container
3135

32-
Include related projects
36+
```csharp
37+
38+
services.AddOptions().Configure<NotificationOptions>(configuration.GetSection(nameof(NotificationOptions)));
39+
services.AddOptions().Configure<SmtpOptions>(configuration.GetSection(nameof(SmtpOptions)));
40+
41+
services.AddTransient<ITemplateProvider, FileBasedTemplateProvider>();
42+
services.AddTransient<IMessagingService, MailKitMessagingService>();
43+
services.AddTransient<ITemplateContentRenderer, HandleBarsTemplateContentRenderer>();
44+
services.AddTransient<ITemplateRenderer, TemplateRenderer>();
45+
services.AddTransient<INotificationService, NotificationService>();
46+
47+
```
48+
49+
You should notice every one of the services can be customize in order to implement something different.
50+
51+
Also the following sections need to be added to the _appsettings.json_ file.
52+
53+
```json
54+
"NotificationOptions": {
55+
"Sender": "sender email address",
56+
"Profiles": [
57+
{
58+
"ProfileName": "profile name",
59+
"Audiences": [
60+
{"Email": "email or distribution group name" }
61+
]
62+
},
63+
],
64+
"Templates": [
65+
{
66+
"Name": "template name",
67+
"Path": "template default location relative to application"
68+
},
69+
]
70+
},
71+
"SmtpOptions": {
72+
"Server": "Smtp server name",
73+
"LocalDomain": "ICGPLC",
74+
"Port": "Optional. Default to 25"
75+
}
76+
77+
```
78+
79+
Then you can inject **INotificationService** to your classes to be able to send the email.
80+
81+
**Note:** Use Relay Messaging so there is no setup to be made on application servers to support smtp. Also the sender can be a fake email address or even a descriptive email address.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<Description>MailKit implementation for Easify Notification Messaging</Description>
6+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
7+
<Authors>Mohammad Moattar</Authors>
8+
<Company>ICG</Company>
9+
<PackageProjectUrl>https://github.com/icgam/Easify.Extensions.Notifications</PackageProjectUrl>
10+
<RepositoryUrl>https://github.com/icgam/Easify.Extensions.Notifications</RepositoryUrl>
11+
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
12+
<Version>1.0.0</Version>
13+
<LangVersion>8</LangVersion>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="MailKit" Version="2.10.0" />
18+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.9" />
19+
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.9" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<ProjectReference Include="..\Easify.Extensions.Notifications.Messaging\Easify.Extensions.Notifications.Messaging.csproj" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Easify.Extensions.Notifications.Messaging;
5+
using Easify.Extensions.Notifications.Messaging.Configuration;
6+
using Easify.Extensions.Notifications.Messaging.Exceptions;
7+
using MailKit.Net.Smtp;
8+
using MailKit.Security;
9+
using Microsoft.Extensions.Logging;
10+
using Microsoft.Extensions.Options;
11+
using MimeKit;
12+
13+
// This software is part of the Easify framework
14+
// Copyright (C) 2019 Intermediate Capital Group
15+
//
16+
// This program is free software: you can redistribute it and/or modify
17+
// it under the terms of the GNU Affero General Public License as published by
18+
// the Free Software Foundation, either version 3 of the License, or
19+
// (at your option) any later version.
20+
//
21+
// This program is distributed in the hope that it will be useful,
22+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
// GNU Affero General Public License for more details.
25+
//
26+
// You should have received a copy of the GNU Affero General Public License
27+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
28+
29+
namespace Easify.Extensions.Notifications.Messaging.MailKit
30+
{
31+
public sealed class MailKitMessagingService : IMessagingService
32+
{
33+
private readonly ILogger<MailKitMessagingService> _logger;
34+
private readonly SmtpOptions _smtpOptions;
35+
36+
public MailKitMessagingService(IOptions<SmtpOptions> smtpOptionsAccessor, ILogger<MailKitMessagingService> logger)
37+
{
38+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
39+
_smtpOptions = smtpOptionsAccessor?.Value ?? throw new ArgumentNullException(nameof(smtpOptionsAccessor));
40+
}
41+
42+
public async Task SendAsync(Message message)
43+
{
44+
if (message == null) throw new ArgumentNullException(nameof(message));
45+
46+
EnsureOptions();
47+
48+
var emailMessage = new MimeMessage();
49+
50+
emailMessage.From.Add(CreateAddress(message.Sender));
51+
emailMessage.To.AddRange(message.Recipients.Select(CreateAddress).ToList());
52+
emailMessage.Subject = message.Subject;
53+
emailMessage.Body = new TextPart("html") {Text = message.Content};
54+
55+
await SendEmailMessageAsync(emailMessage);
56+
}
57+
58+
private void EnsureOptions()
59+
{
60+
if (string.IsNullOrWhiteSpace(_smtpOptions.LocalDomain))
61+
throw new SmtpOptionsException(nameof(_smtpOptions.LocalDomain));
62+
63+
if (string.IsNullOrWhiteSpace(_smtpOptions.Server))
64+
throw new SmtpOptionsException(nameof(_smtpOptions.Server));
65+
}
66+
67+
private async Task SendEmailMessageAsync(MimeMessage message)
68+
{
69+
using (var client = new SmtpClient())
70+
{
71+
client.LocalDomain = _smtpOptions.LocalDomain;
72+
await client.ConnectAsync(_smtpOptions.Server, _smtpOptions.Port, SecureSocketOptions.None);
73+
await client.SendAsync(message);
74+
await client.DisconnectAsync(true);
75+
}
76+
}
77+
78+
private MailboxAddress CreateAddress(EmailAddress recipient)
79+
{
80+
if (recipient == null) throw new ArgumentNullException(nameof(recipient));
81+
82+
return new MailboxAddress(recipient.Name ?? recipient.Email, recipient.Email);
83+
}
84+
}
85+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This software is part of the Easify framework
2+
// Copyright (C) 2019 Intermediate Capital Group
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace Easify.Extensions.Notifications.Messaging.Configuration
18+
{
19+
public sealed class NotificationAudience
20+
{
21+
public string Email { get; set; }
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This software is part of the Easify framework
2+
// Copyright (C) 2019 Intermediate Capital Group
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace Easify.Extensions.Notifications.Messaging.Configuration
18+
{
19+
public sealed class NotificationOptions
20+
{
21+
public string Sender { get; set; }
22+
public NotificationProfile[] Profiles { get; set; } = { };
23+
public NotificationTemplate[] Templates { get; set; } = { };
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This software is part of the Easify framework
2+
// Copyright (C) 2019 Intermediate Capital Group
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace Easify.Extensions.Notifications.Messaging.Configuration
18+
{
19+
public sealed class NotificationProfile
20+
{
21+
public string ProfileName { get; set; }
22+
public NotificationAudience[] Audiences { get; set; }
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This software is part of the Easify framework
2+
// Copyright (C) 2019 Intermediate Capital Group
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace Easify.Extensions.Notifications.Messaging.Configuration
18+
{
19+
public static class NotificationProfileNames
20+
{
21+
public const string DefaultProfile = "DefaultProfile";
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This software is part of the Easify framework
2+
// Copyright (C) 2019 Intermediate Capital Group
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace Easify.Extensions.Notifications.Messaging.Configuration
18+
{
19+
public class NotificationTemplate
20+
{
21+
public string Name { get; set; }
22+
public string Path { get; set; }
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This software is part of the Easify framework
2+
// Copyright (C) 2019 Intermediate Capital Group
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace Easify.Extensions.Notifications.Messaging.Configuration
18+
{
19+
public sealed class SmtpOptions
20+
{
21+
public string Server { get; set; }
22+
public int Port { get; set; } = 25;
23+
public string LocalDomain { get; set; }
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This software is part of the Easify framework
2+
// Copyright (C) 2019 Intermediate Capital Group
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
using FluentValidation;
18+
19+
namespace Easify.Extensions.Notifications.Messaging.Configuration.Validators
20+
{
21+
public class NotificationAudienceValidator : AbstractValidator<NotificationAudience>
22+
{
23+
public NotificationAudienceValidator()
24+
{
25+
RuleFor(m => m.Email).NotEmpty().EmailAddress();
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)