---
# System prepended metadata

title: SendGrid を使用して C# 空メールを送信する 2023-06-22
tags: [メール送信, C#, SendGrid]

---

---
lang: ja-jp
breaks: true
---
## *.csproj

```xml=
・・・
  <ItemGroup>
    <PackageReference Include="SendGrid" Version="9.28.1" />
  </ItemGroup>
・・・
```

## Program.cs

```csharp=
await Execute();

static async Task Execute()
{
    //var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
    var apiKey = "XXXXXXXXXXXXXXXXXXXXXX";

    var client = new SendGridClient(apiKey);
    var from = new EmailAddress("XXXXXXXX@gmail.com", "テスト送信者の名前");
    var subject = "SendGridを使用したメール送信のテスト";
    var to = new EmailAddress("YYYYYYYY@gmail.com", "宛先の名称");
    var plainTextContent = "C#でも、どこでも簡単にできる。";
    var htmlContent = "<strong>C#でも、どこでも簡単にできる。</strong>";
    var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
    var response = await client.SendEmailAsync(msg);
}
```
