YGGraphMail 1.1.0
dotnet add package YGGraphMail --version 1.1.0
NuGet\Install-Package YGGraphMail -Version 1.1.0
<PackageReference Include="YGGraphMail" Version="1.1.0" />
paket add YGGraphMail --version 1.1.0
#r "nuget: YGGraphMail, 1.1.0"
// Install YGGraphMail as a Cake Addin #addin nuget:?package=YGGraphMail&version=1.1.0 // Install YGGraphMail as a Cake Tool #tool nuget:?package=YGGraphMail&version=1.1.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 from App Registration
1. In Azure Portal https://portal.azure.com, login and search for App Registration, click that
2. Click +New Registration
3. Give app name the same name as the email account, and click Register
4. Take the Client ID and Tenant Id, along with Email address and password, and pass that to OAuth2DTO object
5. Click Api permission on the left pane of App Registration page
6. Click +Add permission
7. Click the big Microsoft Graph icon / button
8. Select everything under Mail, IMap, and SMTP. Both under Delegated Permission and APplication Permission
9. Approve the consent
10. Click on Authentication on the left pane of App Registration page
11. Click +Add Platform
12. Select Mobile and Desktop application, and add that
13. Check all Redirect URIs
14. Switch "Allow public client flows" to "Yes"
15. Save
The setup for Graph Email is very tricky
because it requires a lot of service addition and setting in Azure portal
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 | Versions 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. |
-
net8.0
- Microsoft.Graph (>= 5.53.0)
- Microsoft.Identity.Client (>= 4.61.0)
- MimeKit (>= 4.5.0)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added Graph setup instructions in Readme.md