Skip to content

About

Helpers and services for sending messages (email, SMS) and Telegram-related types, with DI-friendly configuration.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

License GitHub Release Date NuGetVersion NugetDownloads Coverage issues .NET PR

Size GitHub contributors GitHub commits since latest release (by date) Activity Activity Activity

Cross.Messaging

A set of libraries for sending messages across multiple channels.

Current focus:

  • Email (Cross.Messaging.Email.*)
  • SMS (Cross.Messaging.Sms.*)
  • Telegram (Cross.Messaging.Telegram.*)
  • Viber (Cross.Messaging.Viber.*)
  • WatsApp (Cross.Messaging.WatsApp.*)

Supported targets: .NET Standard 2.1, .NET 6, .NET 7, .NET 8, .NET 9, .NET 10

Install NuGet package

Install the Cross.Messaging NuGet package into your .NET project:

Install-Package Cross.Messaging

or

dotnet add package Cross.Messaging

Quick start

Register the email sender in DI

services.AddEmailSender(configuration);

AddEmailSender:

  • binds the MessagingEmail configuration section to MessagingEmailOptions
  • registers IEmailSenderService -> EmailSenderService (scoped)

Email sender API

IEmailSenderService currently exposes:

  • SendAsync(toName, toEmail, subject, body, cancellationToken)
  • SendAsync(toName, toEmail, subject, body, priority, cancellationToken)
  • SendAsync(toName, toEmail, subject, textBody, htmlBody, cancellationToken)
  • SendAsync(toName, toEmail, subject, textBody, htmlBody, priority, cancellationToken)
  • SendAsync(toName, toEmail, subject, textBody, htmlBody, attachments, cancellationToken)
  • SendAsync(toName, toEmail, subject, textBody, htmlBody, priority, attachments, cancellationToken)
  • SendAsyncWithContentIds(toName, toEmail, subject, textBody, htmlBody, attachments, contentIdMap, cancellationToken)
  • SendAsyncWithContentIds(toName, toEmail, subject, textBody, htmlBody, priority, attachments, contentIdMap, cancellationToken)

If you use attachment overloads, pass IReadOnlyCollection<IFormFile>? (Microsoft.AspNetCore.Http).

Example:

await emailSender.SendAsync(
    toName: "User",
    toEmail: "[email protected]",
    subject: "Hello",
    textBody: "Plain text body",
    htmlBody: "<p>HTML body</p>",
    priority: XMessagePriority.Normal,
    attachments: null,
    cancellationToken: cancellationToken);

Sample email configuration

{
  "MessagingEmail": {
    "SmtpHost": "smtp.example.com",
    "SmtpPort": 587,
    "UseSsl": true,
    "SecureSocket": "StartTls",
    "CheckCertificateRevocation": true,
    "SmtpLogin": "login",
    "SmtpPassword": "password",
    "FromUserName": "App Messaging",
    "FromUserAddress": "[email protected]",
    "RecipientOverride": "",
    "BccRecipients": [
      {
        "Name": "Audit Bot",
        "Email": "[email protected]"
      }
    ]
  }
}

CheckCertificateRevocation applies to MailKit send flows (text+HTML / attachments). Default is true. Set to false only when CRL/OCSP checks fail in constrained networks (for example local/VPN).

BccRecipients is optional; when configured, each entry is added to outgoing messages as BCC.

Entries with empty Email are ignored.

Register the SMS sender in DI

services.AddSmsSender(configuration);

AddSmsSender:

  • binds the MessagingSms configuration section to MessagingSmsOptions
  • registers ISmsSenderService -> SmsSenderService (scoped)
  • registers an SMS provider only when it is referenced by DefaultProvider or CountryProviders and also has settings under MessagingSms:Providers (evaluated at startup; restart if a reload references a provider that was not registered then)

ISmsSenderService.SendAsync(destination, body, cancellationToken) expects destination in E.164 (+...). The country is resolved from the calling code (longest-prefix match); NANP +1 maps to US.

DefaultProvider is used for all destinations unless the resolved country has an override in CountryProviders (list only exceptions there, not every country).

Sample SMS configuration

{
  "MessagingSms": {
    "DefaultProvider": "Telnyx",
    "CountryProviders": {
      "RO": "SMSClub"
    },
    "Providers": {
      "Telnyx": {
        "ApiKey": "KEY",
        "From": "+15551234567",
        "MessagingProfileId": ""
      },
      "SMSClub": {
        "ApiKey": "TOKEN",
        "From": "MySender"
      }
    }
  }
}

MessagingProfileId is optional and used for Telnyx modes that require a messaging profile (for example number pool / alphanumeric sender).

For SMSClub, ApiKey is the Bearer token and From is the alphanumeric sender name (src_addr).

Unit tests

Tests use the Given_When_Then naming style:

  • Given — context and preconditions.
  • When — the action under test.
  • Then — the expected outcome.

Example: Given_ValidSmtpOptions_When_SendAsync_Then_UsesConfiguredHost.

Tests live in Cross.Messaging.Tests/ (for example Email/, Sms/).

Available NUnit categories:

  • Unit
  • Integration

Run only unit tests (NUnit Category=Unit):

dotnet test Cross.Messaging.Tests/Cross.Messaging.Tests.csproj --filter "Category=Unit"

Run only integration tests (NUnit Category=Integration):

dotnet test Cross.Messaging.Tests/Cross.Messaging.Tests.csproj --filter "Category=Integration"

Code coverage

Run tests with code coverage (opencover):

dotnet test Cross.Messaging.Tests/Cross.Messaging.Tests.csproj \
 --collect:"XPlat Code Coverage" \
 --results-directory ./TestResults \
 -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover

About

Helpers and services for sending messages (email, SMS) and Telegram-related types, with DI-friendly configuration.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages