-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate SendGrid with HttpClientFactory (#922)
* Integrate SendGrid with HttpClientFactory - Introduce new SendGrid.Extensions.DependencyInjection package for HttpClientFactory integrations * Update SendGrid.Tests - Update NuGet packages - Cleanup Properties * Add Solution Items * Renamed `Example.cs` to `Program.cs` - `Program.cs` is the convention of C# console application entry point * Build .NET Framework libraries on Linux without Mono - https://andrewlock.net/using-reference-assemblies-to-build-net-framework-libararies-on-linux-without-mono/ * Simplify Examples - Upgrade to SDK style csproj - Code cleanup * Simplify Makefile * Update README.md - Add `SendGrid.Extensions.DependencyInjection` usage - Fixes typo - Fixes Markdown syntax warnings - Simplify examples * Use .NET Core CLI for *.nupkg deployment - https://docs.microsoft.com/dotnet/core/tools/dotnet-nuget-push
- Loading branch information
Showing
21 changed files
with
506 additions
and
482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.0</TargetFramework> | ||
<AssemblyName>ExampleCoreProject</AssemblyName> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
<PackageId>ExampleCoreProject</PackageId> | ||
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\SendGrid\SendGrid.csproj" /> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="appsettings.*.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' "> | ||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" /> | ||
<PackageReference Include="System.Net.Http" Version="4.3.2" /> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.4" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\SendGrid.Extensions.DependencyInjection\SendGrid.Extensions.DependencyInjection.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SendGrid; | ||
using SendGrid.Extensions.DependencyInjection; | ||
using SendGrid.Helpers.Mail; | ||
|
||
namespace Example | ||
{ | ||
internal class Program | ||
{ | ||
private static IConfiguration Configuration { get; set; } | ||
|
||
private static async Task Main() | ||
{ | ||
var env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"; | ||
Configuration = new ConfigurationBuilder() | ||
.AddJsonFile("appsettings.json", optional: true) | ||
.AddJsonFile($"appsettings.{env}.json", optional: true) | ||
.Build(); | ||
var services = ConfigureServices(new ServiceCollection()).BuildServiceProvider(); | ||
var client = services.GetRequiredService<ISendGridClient>(); | ||
var from = new EmailAddress(Configuration.GetValue("SendGrid:From", "[email protected]"), "Example User"); | ||
var to = new EmailAddress(Configuration.GetValue("SendGrid:To", "[email protected]"), "Example User"); | ||
var msg = new SendGridMessage | ||
{ | ||
From = from, | ||
Subject = "Sending with Twilio SendGrid is Fun" | ||
}; | ||
msg.AddContent(MimeType.Text, "and easy to do anywhere, even with C#"); | ||
msg.AddTo(to); | ||
if (Configuration.GetValue("SendGrid:SandboxMode", false)) | ||
{ | ||
msg.MailSettings = new MailSettings | ||
{ | ||
SandboxMode = new SandboxMode | ||
{ | ||
Enable = true | ||
} | ||
}; | ||
} | ||
Console.WriteLine($"Sending email with payload: \n{msg.Serialize()}"); | ||
var response = await client.SendEmailAsync(msg).ConfigureAwait(false); | ||
|
||
Console.WriteLine($"Response: {response.StatusCode}"); | ||
Console.WriteLine(response.Headers); | ||
} | ||
|
||
private static IServiceCollection ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddSendGrid(options => { options.ApiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? Configuration["SendGrid:ApiKey"]; }); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.