TPJ.Email 2.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TPJ.Email --version 2.2.1
NuGet\Install-Package TPJ.Email -Version 2.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TPJ.Email" Version="2.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TPJ.Email --version 2.2.1
#r "nuget: TPJ.Email, 2.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TPJ.Email as a Cake Addin
#addin nuget:?package=TPJ.Email&version=2.2.1

// Install TPJ.Email as a Cake Tool
#tool nuget:?package=TPJ.Email&version=2.2.1

TPJ.Email

Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 is compatible.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.0 316 1/22/2023
3.0.0 1,122 8/20/2017
2.5.0 1,058 5/4/2017
2.3.0 1,299 10/6/2016
2.2.1 1,180 8/30/2016
1.0.0 1,964 4/19/2016

New - Ability to add attachments to e-mails

ASP.Net Core Website Set up

Create appsettings.json file and add the following

{
 "TPJ": {
   "Email": {
     "SmtpClient": "",
     "SmtpUser": "",
     "SmtpPassword": "",
     "EmailFrom": "",
     "Port": "",
     "EnableSSL": ""
   }
 }
}


SmtpClient - SMTP server which e-mails will be sent from

SmtpUser - (Not required) send e-mail using the given user name and password
SmtpPassword - (Not required) send e-mail using the given user name and password

EmailFrom - E-mails are sent from this account

Port - (Not required) Port to send on

EnableSSL - (Not required) Send using SSL


For Websites -

Open StartUp.cs file and go to ConfigureServices

Add the following (I do not need port or enableSSL so have not included them)

services.Configure<TPJ.Email.Models.EmailProperties>(options =>
{
   options.SmtpClient = Configuration["TPJ:Email:SmtpClient"];
   options.EmailFrom = Configuration["TPJ:Email:EmailFrom"];
   options.SmtpUser = Configuration["TPJ:Email:SmtpUser"];
   options.SmtpPassword = Configuration["TPJ:Email:SmtpPassword"];
});

services.AddTransient<TPJ.Email.Interfaces.IEmailer, TPJ.Email.Emailer>();

Then using DI within asp.net core you can call IEmailerlike so

private readonly TPJ.Email.Interfaces.IEmailer _emailer;

public HomeController(TPJ.Email.Interfaces.IEmailer emailer)
{
   _emailer = emailer;
}

Then within an IActionResult you might have this

public IActionResult About()
{
 try
 {
   _logger.InformationLog("About Page Loaded", Enums.LogEnvironment.Development);

   return View();

 }
 catch (Exception e)
 {
   _emailer.SendEmail("test@test.co.uk", "Test Email", "Test Message", false);

   return RedirectToAction(nameof(Error));
 }
}

Here an e-mail will be sent to test@Test.co.uk with the subject of Test Email saying Test Message. The message content is not html.


ASP.Net Core Console Application

This is how I do it. This may change and I'm sure there are different ways to do this. This is also just to get the logger to work. You may need to change things to get other packages to load.

Add to the top of the program class

public static IEmailer _emailer;

Then add set up method

private static void SetUp()
{
   var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
   
   var configuration = builder.Build();
   _emailer = new Emailer(new TPJ.Email.Models.EmailProperties(configuration));
}

Then call the "SetUp" method from the Main method

public static void Main(string[] args)
{
   SetUp();
}

Then you can call the logger like so

public static void Main(string[] args)
{
   SetUp();

   _emailer.SendEmail("test@test.co.uk", "Test Email", "Test Message", false);
}