Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mail settings / send mailer #465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions starsky/starsky.foundation.mail/Helpers/SendMail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

using System.Threading.Tasks;
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;
using starsky.foundation.platform.Interfaces;
using starsky.foundation.platform.Models;

namespace starsky.foundation.mail.Helpers
{
public class SendMail
{
private readonly AppSettings _appSettings;
private readonly IWebLogger _logger;

public SendMail(AppSettings appSettings, IWebLogger logger)
{
_appSettings = appSettings;
_logger = logger;
}

public async Task SendAsync(string toEmail, string toName, string subject, string plainBodyText)
{
if ( string.IsNullOrWhiteSpace(_appSettings.MailSmtpServer) )
{
_logger.LogInformation("send mail skipped due missing server");
return;
}

var message = new MimeMessage ();
message.From.Add (new MailboxAddress (_appSettings.Name, _appSettings.MailFromEmail));
if ( string.IsNullOrWhiteSpace(toName) ) toName = toEmail;
message.To.Add (new MailboxAddress (toName, toEmail));
message.Subject = subject;

message.Body = new TextPart ("plain") {
Text = plainBodyText
};

using (var client = new SmtpClient ()) {
await client.ConnectAsync (_appSettings.MailSmtpServer, _appSettings.MailSmtpPort, _appSettings.MailSmtpUseSsl);

// Note: only needed if the SMTP server requires authentication
if ( !string.IsNullOrWhiteSpace(_appSettings.MailSmtpUserName) && !string.IsNullOrWhiteSpace(_appSettings.MailSmtpPassword))
{
await client.AuthenticateAsync (_appSettings.MailSmtpUserName, _appSettings.MailSmtpPassword);
}

await client.SendAsync(message);
await client.DisconnectAsync (true);
}
}

}
}
15 changes: 15 additions & 0 deletions starsky/starsky.foundation.mail/starsky.foundation.mail.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MailKit" Version="2.15.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\starsky.foundation.platform\starsky.foundation.platform.csproj" />
</ItemGroup>

</Project>
14 changes: 13 additions & 1 deletion starsky/starsky.foundation.platform/Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,19 @@ public string ApplicationInsightsInstrumentationKey {
/// Use always UNIX style
/// </summary>
public List<string> SyncIgnore { get; set; } = new List<string>{"/lost+found"};


public string MailSmtpServer { get; set; } = "mail.kpnmail.nl";

public string MailFromEmail { get; set; } = "noreply@qdraw.nl";

public int MailSmtpPort { get; set; } = 25;

public bool MailSmtpUseSsl{ get; set; } = false;

public string MailSmtpUserName { get; set; }

public string MailSmtpPassword { get; set; }

// -------------------------------------------------
// ------------------- Modifiers -------------------
// -------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions starsky/starsky.sln
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "starsky.foundation.thumbnai
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "starskythumbnailmetacli", "starskythumbnailmetacli\starskythumbnailmetacli.csproj", "{E0559015-77F2-4135-9AF5-41EBEA6C2C9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "starsky.foundation.mail", "starsky.foundation.mail\starsky.foundation.mail.csproj", "{527A889F-5EC3-496D-B540-CD8753AD998C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -246,6 +248,10 @@ Global
{E0559015-77F2-4135-9AF5-41EBEA6C2C9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0559015-77F2-4135-9AF5-41EBEA6C2C9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0559015-77F2-4135-9AF5-41EBEA6C2C9C}.Release|Any CPU.Build.0 = Release|Any CPU
{527A889F-5EC3-496D-B540-CD8753AD998C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{527A889F-5EC3-496D-B540-CD8753AD998C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{527A889F-5EC3-496D-B540-CD8753AD998C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{527A889F-5EC3-496D-B540-CD8753AD998C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -289,5 +295,6 @@ Global
{EE4E6FCA-9E87-4E31-A137-8D1809FFED54} = {1C1EB4A5-08D0-4014-AE1F-962642A4E5D3}
{1FD59380-AC19-4AF1-A5A5-F295A2844B11} = {1C1EB4A5-08D0-4014-AE1F-962642A4E5D3}
{E0559015-77F2-4135-9AF5-41EBEA6C2C9C} = {B974FC20-C3EE-4EB0-AF25-F0D8DA2C28D7}
{527A889F-5EC3-496D-B540-CD8753AD998C} = {1C1EB4A5-08D0-4014-AE1F-962642A4E5D3}
EndGlobalSection
EndGlobal
37 changes: 37 additions & 0 deletions starsky/starsky/Controllers/TestController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using starsky.foundation.mail.Helpers;
using starsky.foundation.platform.Interfaces;
using starsky.foundation.platform.Models;

namespace starsky.Controllers
{
[Authorize]
public class TestController : Controller
{
private readonly AppSettings _appSettings;
private readonly IWebLogger _logger;

public TestController(AppSettings appSettings, IWebLogger logger)
{
_appSettings = appSettings;
_logger = logger;
}

[HttpGet("/test/test")]
public async Task<IActionResult> Tetung(string f)
{
var text = @"Hey Chandler,

I just wanted to let you know that Monica and I were going to go play some paintball, you in?

-- Joey";

await new SendMail(_appSettings, _logger).SendAsync("dionvanvelde@gmail.com","","test", text);
return Ok();
}


}
}
1 change: 1 addition & 0 deletions starsky/starsky/starsky.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<ProjectReference Include="..\starsky.foundation.accountmanagement\starsky.foundation.accountmanagement.csproj" />
<ProjectReference Include="..\starsky.foundation.database\starsky.foundation.database.csproj" />
<ProjectReference Include="..\starsky.foundation.http\starsky.foundation.http.csproj" />
<ProjectReference Include="..\starsky.foundation.mail\starsky.foundation.mail.csproj" />
<ProjectReference Include="..\starsky.foundation.realtime\starsky.foundation.realtime.csproj" />
<ProjectReference Include="..\starsky.foundation.sync\starsky.foundation.sync.csproj" />
<ProjectReference Include="..\starsky.foundation.thumbnailmeta\starsky.foundation.thumbnailmeta.csproj" />
Expand Down