Rayab.Email 1.0.1

dotnet add package Rayab.Email --version 1.0.1
NuGet\Install-Package Rayab.Email -Version 1.0.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="Rayab.Email" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rayab.Email --version 1.0.1
#r "nuget: Rayab.Email, 1.0.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 Rayab.Email as a Cake Addin
#addin nuget:?package=Rayab.Email&version=1.0.1

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

Email Sending

With this library, you can send emails to your ASP.Net Core projects.

Sending an email is very simple using this.

You can set up your favorite email templates. You can set up sync with your database by Rayab.ApplicationDbContext library.

This means is: After sending each email, it insert a record in the database, checks the email addresses in its database to see if it already exists, or needs to insert a new record about email addresses.

Can from the body of an email, using an HTML string or Razor Pages .cshtml views.

See How to use and Enjoy this:

How to use:

  • Set up in startup.cs

Define the following code in startup.cs:

private string _webRootPath = "";
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
    Configuration = configuration;
    _webRootPath = env.WebRootPath;
}
public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<RMailConfiguration>(new RMailConfiguration()
    {
        Server = "smtp.gmail.com",
        Port = 587,
        Socket = SecureSocketOptions.StartTls,
        Username = "yourSmtpUsername",
        Password = "yourSmtpPassword",
        ConnectionString = Configuration.GetConnectionString("DefaultConnection"),
        SaveAttachmentFolderName = "EmailAttachments",
        SyncWithDatabase = false,
        WebRootPath = _webRootPath
    });
}
  • Sending email: Define the following code in controller:
private readonly RMailConfiguration _mailConfiguration;
public HomeController(RMailConfiguration mailConfiguration)
{
    _mailConfiguration = mailConfiguration;
}

And use the following code in your action:

new RMail()
    .Configuration(_mailConfiguration)
    .Sender("example0@domain.com")
    .To("example@domain.com")
    .To("example1@domain.com", "example4@domain.com")
    .CC("example2@domain.com", "example5@domain.com")
    .BCC("example3@domain.com", "example6@domain.com")
    .Body(
        "<h1>Hello Dear Rayab</h1> <br/> How are you today my friend? <small> Did you know that? </small>")
    .Subject("Rayab.EmailCore Testing")
    .Send();

I show you how I work with this library for better results.But, let me first show you how it works in every section.

_webRootPath

We need this variable to get "WebRootPath".

If you want to save Attachment files, We need to know where you want to save those files. So we need "WebRootPath" to save files.So if you want to save Attachment files, You must fill this field:

SaveAttachmentFolderName = "EmailAttachments"

after filling this field, We create a folder with this name and Save all Attachment files there.

Synchronize with the database by this field:

SyncWithDatabase = true

If this field set to true, Your database sync with new email addresses and every email recorded in the database. But if you don't want to sync with your database, or you do not use our Rayab.ApplicationDbContext, You can set this field to false and do not need to ConnectionString field too.

Abot new RMail():

To initialize RMail, You need to Configuration method. This method has two sections. Section 1: Using _mailConfiguration alone. This use is for string HTML body. Section 2: Using ** _mailConfiguration** and Controller as this. This use is for EmailTemplates from your database or a new instance of EmailTemplates.

So you can use like this:

new RMail()
    .Configuration(_mailConfiguration, this);

To use email sending we used mimeKit and mailKit. That library needs to Sender or From. To initialize that, You must use this method:

.Sender("Rayab.Developer@Gmail.com")
// OR
.Sender("Rayab", "Rayab.Developer@Gmail.Com")// for new MailboxAddress
// OR
.Sender("Rayab.Developer@Gmail.Com", "Rayab2.Developer2@Gmail.Com")// for many senders
// OR
.Sender(new MailboxAddress("Mr Rayab", "Rayab.Developer@Gmail.Com"))// for new MailboxAddress
// OR
.Sender(new MailboxAddress("Mr Rayab", "Rayab.Developer@Gmail.Com"), new MailboxAddress("Mr Rayab", "Rayab.Developer@Gmail.Com"))// for many MailboxAddress

You can use To,CC, BCC like this too:

.To("Rayab.Developer@Outlook.com")
.CC("Rayab.Developer@Outlook.com")
.BCC("Rayab.Developer@Outlook.com")
// OR
.To("Rayab.Developer@Outlook.com", "Rayab2.Developer2@Outlook.com", "Rayab3.Developer3@Outlook.com")// For many emails.

In the body, you can use the string HTML or email template. We show you both of those.

  • string HTML:
.Body("Hello World!")
// OR
.Body("<h1>Hello World!")
  • Email Templates: To using the EmailTemplates class, You must define this object first:
var template = new EmailTemplates()
    {
        TemplateViewPath = "/wwwroot/template/email/_EmailTemplate.cshtml",
        IsPartial = true
    };

Or you can get this EmailTemplates from your database, We defined this table in Rayab.Identity and Rayab.ApplicationDbContext. To insert a new record of EmailTemplates into the database, You must fill EmailTemplates like this:

var template = new EmailTemplates()
    {
        TemplateViewPath = "/wwwroot/template/email/_EmailTemplate.cshtml",
        IsPartial = true
    };
_db.EmailTemplates.Add(template);

Now, With or Without inserting in the database, you can use template in RMail like this:

.Body(template)

If your View needs to model, Use like this:

.Body(template, model)

If you have attachment files, We supported IFormFile[], Use like this:

.Attachment(file1, file2, file3)
// OR use an Array of file
.Attachment(files)

More Information About EmailTemplates.cs

In this class, we have three fields. TemplateViewName, TemplateViewPath and IsPartial If you have a template anywhere like wwwroot folder, You must use and fill only TemplateViewPath like this:

TemplateViewPath = "/wwwroot/template/email/_EmailTemplate.cshtml"

But if you have a template in your Views folder in the project and your view is in the Shared folder or Shared/Some folder, Use like this:

TemplateViewName = "Some/ViewName"
// OR
TemplateViewName = "ViewName" // is in the Shared folder.

If your view is a PartialView, Set IsPartial to true, By this field, Razor not compiled the layout. ( Actually, you know this )

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
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
1.0.1 534 12/2/2020
1.0.0 394 11/30/2020

Successful building and testing.