Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SendGrid custom host support #369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class FluentEmailSendGridBuilderExtensions
{
public static FluentEmailServicesBuilder AddSendGridSender(this FluentEmailServicesBuilder builder, string apiKey, bool sandBoxMode = false)
public static FluentEmailServicesBuilder AddSendGridSender(this FluentEmailServicesBuilder builder, string apiKey, string host = null, bool sandBoxMode = false)
{
builder.Services.TryAdd(ServiceDescriptor.Singleton<ISender>(_ => new SendGridSender(apiKey, sandBoxMode)));
builder.Services.TryAdd(ServiceDescriptor.Singleton<ISender>(_ => new SendGridSender(apiKey, host, sandBoxMode)));
return builder;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Senders/FluentEmail.SendGrid/SendGridSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading;
using System.Threading.Tasks;
using FluentEmail.Core;
using FluentEmail.Core.Interfaces;
using FluentEmail.Core.Models;
using SendGrid;
using SendGrid.Helpers.Mail;
Expand All @@ -16,11 +15,13 @@ namespace FluentEmail.SendGrid
public class SendGridSender : ISendGridSender
{
private readonly string _apiKey;
private readonly string _host;
private readonly bool _sandBoxMode;

public SendGridSender(string apiKey, bool sandBoxMode = false)
public SendGridSender(string apiKey, string host = null, bool sandBoxMode = false)
{
_apiKey = apiKey;
_host = host;
_sandBoxMode = sandBoxMode;
}
public SendResponse Send(IFluentEmail email, CancellationToken? token = null)
Expand Down Expand Up @@ -145,7 +146,7 @@ private async Task<SendGridMessage> BuildSendGridMessage(IFluentEmail email)

private async Task<SendResponse> SendViaSendGrid(SendGridMessage mailMessage, CancellationToken? token = null)
{
var sendGridClient = new SendGridClient(_apiKey);
var sendGridClient = new SendGridClient(_apiKey, _host);
var sendGridResponse = await sendGridClient.SendEmailAsync(mailMessage, token.GetValueOrDefault());

var sendResponse = new SendResponse();
Expand Down
2 changes: 1 addition & 1 deletion test/FluentEmail.Core.Tests/SendGridSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void SetUp()
{
if (string.IsNullOrWhiteSpace(apiKey)) throw new ArgumentException("SendGrid Api Key needs to be supplied");

var sender = new SendGridSender(apiKey, true);
var sender = new SendGridSender(apiKey, null, true);
Email.DefaultSender = sender;
}

Expand Down