private static async Task SendMailWithOptions_Test() { var options = new SendGridMailerServiceOptions { BodyHtmlFormat = "<html><body>{0}<hr/>This is footer</body></html>", BodyTextFormat = "{0}\r\n--\r\nThis is footer", SubjectFormat = "[test] {0}", DefaultFrom = new MailAddressDto("*****@*****.**", "Example From"), ApiKey = _apiKey }; var mx = new SendGridMailerService(options); var msg = new MailMessageDto { Subject = "Žluťoučký kůň úpěl ďábelské ódy - subject", BodyText = "Žluťoučký kůň úpěl ďábelské ódy - text.", BodyHtml = "<p>Žluťoučký kůň úpěl ďábelské ódy <b>v HTML</b>.</p>" }; msg.To.Add(new MailAddressDto("*****@*****.**", "Example Recipient")); using (var ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("Test attachment file"))) { msg.Attachments.Add(new AttachmentDto { Name = "attachment.txt", MimeType = "text/plain", Stream = ms }); await mx.SendMessageAsync(msg); } }
public static IServiceCollection AddSendGridMailerService(this IServiceCollection services, string apiKey) { if (apiKey == null) { throw new ArgumentNullException(nameof(apiKey)); } if (string.IsNullOrWhiteSpace(apiKey)) { throw new ArgumentException("Value cannot be empty or whitespace only string.", nameof(apiKey)); } var options = new SendGridMailerServiceOptions { ApiKey = apiKey }; services.AddSingleton <IMailerService>(new SendGridMailerService(options)); return(services); }
public static IServiceCollection AddSendGridMailerService(this IServiceCollection services, SendGridMailerServiceOptions options) { services.AddSingleton <IMailerService>(new SendGridMailerService(options)); return(services); }