YGGraphMail 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package YGGraphMail --version 1.0.0                
NuGet\Install-Package YGGraphMail -Version 1.0.0                
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="YGGraphMail" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add YGGraphMail --version 1.0.0                
#r "nuget: YGGraphMail, 1.0.0"                
#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 YGGraphMail as a Cake Addin
#addin nuget:?package=YGGraphMail&version=1.0.0

// Install YGGraphMail as a Cake Tool
#tool nuget:?package=YGGraphMail&version=1.0.0                

YGSendMail

A simple email sending functionality that uses Microsoft Graph to connect, authenticate, build and send the emails

Installation

Include this package in the project file
Be sure to setup your Corporate email in Azure Portal, obtain Graph TenantId and ClientId, and enable "Mail.Send" scope in App Registration

Usage

Console:

OAuth2DTO oauth2DTO = new OAuth2DTO()
{ ClientId = clientId,
TenantId = tenantId,
UserName = username,
Password = password
};

IGraphEmail ge = new GraphEmail(oauth2DTO, tokenFile);
ge.OnError += Ge_OnError;

void Ge_OnError(string c, string m, Exception ex)
{
Console.WriteLine($"Error at {c}.{m}. Error message: {ex.Message}");
}

GraphSendEmailDTO eml = new GraphSendEmailDTO()
{
To = new string[] { recipient },
Subject = subject,
Body = body
};
string result = await ge.SendEmailAsync(eml);

Console.WriteLine($"Email result: {result}");

Web:

    OAuth2DTO oauth2DTO = new OAuth2DTO()  
    {  
        ClientId = clientId,  
        TenantId = tenantId,  
        UserName = username,  
        Password = password  
    };  

    IGraphEmailWeb ge = new GraphEmailWeb(oauth2DTO, new string[] { "Mail.Send" });  
    ge.OnGEWebError += Ge_OnError;  

    GraphSendEmailDTO eml = new GraphSendEmailDTO()  
    {  
        To = new string[] { recipient },  
        Subject = subject,  
        Body = body  
    };  
    result = await ge.SendEmailAsync(eml);  

    return Ok($"Email send result: {result}");  

void Ge_OnError(string c, string m, Exception ex)
{
_logger.LogError($"Error at {c}.{m}. Error message: {ex.Message}");
}

Attachments and LinkedResources can be added in eml object Attachments and LinkedResources can be filenames or filenames and byte[] contents

Examples

Test project is provided in the solution to illustrate how instantiate and invoke the email sending functionality

Output:

The output is a string. If there is any error, the output will show the error

Dependencies

Microsoft.Graph Microsoft.Identity.Client MimeKit
Newtonsoft.Json

Contributing

Any new ideas on how to enhance this class without adding much complexity, please adhere to SOLID principle

License

This project is licensed under the MIT License(LICENSE).

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.1.0 129 5/22/2024
1.0.0 108 5/17/2024

Initial Release