-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSendGridCsharpExample.cs
More file actions
39 lines (36 loc) · 2.05 KB
/
SendGridCsharpExample.cs
File metadata and controls
39 lines (36 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Linq;
using System.Collections.Generic;
using SendGrid.Helpers.Mail;
using SendGrid;
using System.Threading.Tasks;
using System.IO;
namespace sendgridjp_csharp_example
{
class SendGridCsharpExample
{
static async Task Main(string[] args)
{
DotNetEnv.Env.Load(".env");
var apiKey = Environment.GetEnvironmentVariable("API_KEY");
var tos = Environment.GetEnvironmentVariable("TOS").Split(',').Select(to => new EmailAddress(to)).ToList();
var from = new EmailAddress(Environment.GetEnvironmentVariable("FROM"), "送信者名");
var subject = "[sendgrid-c#-example] フクロウのお名前はfullnameさん";
var plainTextContent = "familyname さんは何をしていますか?\r\n 彼はplaceにいます。";
var htmlContent = "<strong> familyname さんは何をしていますか?</strong><br />彼はplaceにいます。";
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, subject, plainTextContent, htmlContent);
msg.AddSubstitutions(new Dictionary<string, string> {{"fullname", "田中 太郎"}, {"familyname", "田中"}, {"place", "中野"}}, 0);
msg.AddSubstitutions(new Dictionary<string, string> {{"fullname", "佐藤 次郎"}, {"familyname", "佐藤"}, {"place", "目黒"}}, 1);
msg.AddSubstitutions(new Dictionary<string, string> {{"fullname", "鈴木 三郎"}, {"familyname", "鈴木"}, {"place", "中野"}}, 2);
msg.AddCategory("category1");
msg.AddHeader("X-Sent-Using", "SendGrid-API");
var image = Convert.ToBase64String(File.ReadAllBytes("gif.gif"));
msg.AddAttachment("owl.gif", image, "image/gif", "attachment");
var client = new SendGridClient(apiKey);
var response = await client.SendEmailAsync(msg);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
}
}
}