public MailgunSender(string domainName, string apiKey, MailGunRegion mailGunRegion = MailGunRegion.USA) { _domainName = domainName; _apiKey = apiKey; string url = string.Empty; switch (mailGunRegion) { case MailGunRegion.USA: url = $"https://api.mailgun.net/v3/{_domainName}/"; break; case MailGunRegion.EU: url = $"https://api.eu.mailgun.net/v3/{_domainName}/"; break; default: throw new ArgumentException($"'{mailGunRegion}' is not a valid value for {nameof(mailGunRegion)}"); } _httpClient = new HttpClient() { BaseAddress = new Uri(url) }; _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"api:{_apiKey}"))); }
/// <summary> /// Initiate the MailGun Sender /// </summary> /// <param name="domainName">Domain Name registered with MailGun</param> /// <param name="apiKey">ApiKey from MailGun</param> /// <param name="mailGunRegion">The MailGun Region. Defaults to USA.</param> /// <exception cref="ArgumentException"></exception> public MailGunSender(string domainName, string apiKey, MailGunRegion mailGunRegion = MailGunRegion.USA) { apiKey.ThrowIfNullOrEmpty(nameof(apiKey)); domainName.ThrowIfNullOrEmpty(nameof(domainName)); _domainName = domainName; var url = string.Empty; switch (mailGunRegion) { case MailGunRegion.USA: url = $"https://api.mailgun.net/v3"; break; case MailGunRegion.EU: url = $"https://api.eu.mailgun.net/v3"; break; default: throw new ArgumentException($"'{mailGunRegion}' is not a valid value for {nameof(mailGunRegion)}"); } _restClient = new RestClient { BaseUrl = new Uri(url), Authenticator = new HttpBasicAuthenticator("api", apiKey) }; }
public static BabouEmailServicesBuilder AddMailGunSender(this BabouEmailServicesBuilder builder, string domainName, string apiKey, MailGunRegion mailGunRegion = MailGunRegion.USA) { builder.Services.TryAdd(ServiceDescriptor.Scoped <IBabouSender>(x => new MailGunSender(domainName, apiKey, mailGunRegion))); return(builder); }