---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://sendcorex.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://sendcorex.com/llms.txt"
---

Source: https://sendcorex.com/ja/blog/send-email-dotnet

チュートリアル

# .NET（C#）でSMTP経由でメールを送る

標準のSystem.Net.Mailクライアントか、最新のTLS、async、確実なエラー報告が欲しいならMailKitを使います。

Sendcorexチーム·更新日 2026年9月21日·9分で読めます

## 方法1：System.Net.Mail.SmtpClient

標準搭載ですが、新規開発では非推奨とされています。587番ポートで`EnableSsl = true`を使います：

```
using System.Net;
using System.Net.Mail;

var apiKey = Environment.GetEnvironmentVariable("SENDCOREX_API_KEY")!;

using var client = new SmtpClient("smtp.sendcorex.com", 587)
{
    EnableSsl = true,
    Credentials = new NetworkCredential(apiKey, apiKey),
};

using var message = new MailMessage
{
    From = new MailAddress("hello@yourdomain.com", "Your App"),
    Subject = "Welcome!",
    Body = "<h1>You are in.</h1>",
    IsBodyHtml = true,
};
message.To.Add("customer@example.com");

await client.SendMailAsync(message);
```

## 方法2：MailKit（推奨）

```
dotnet add package MailKit
```

```
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

var apiKey = Environment.GetEnvironmentVariable("SENDCOREX_API_KEY")!;

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Your App", "hello@yourdomain.com"));
message.To.Add(new MailboxAddress("", "customer@example.com"));
message.Subject = "Welcome!";
message.Body = new TextPart("html") { Text = "<h1>You are in.</h1>" };

using var smtp = new SmtpClient();
await smtp.ConnectAsync("smtp.sendcorex.com", 587, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync(apiKey, apiKey);
await smtp.SendAsync(message);
await smtp.DisconnectAsync(true);
```

## どちらを使うべき？

新しいコードではすべてMailKitを使いましょう。どのポートでもTLSを正しく扱い、キャンセル対応の完全なasyncで、活発にメンテナンスされています。`SmtpClient`は、触りたくない既存のコードにだけ残しましょう。

## トラブルシューティング

| 症状 | 対処 |
| --- | --- |
| `Authentication failed` | 環境変数が読み込まれ、usernameとpasswordの両方に使われているか確認してください。 |
| `SmtpProtocolException` | 465番ポートには`StartTls`ではなく`SslOnConnect`が必要です。 |

### このページの内容

数分で最初のメールを送信

無料アカウントを作成し、ドメインを認証して、上のコードを貼り付けましょう。

[無料で始める — 月3,000通](https://app.sendcorex.com/authentication/register?appid=smtp)

クレジットカード不要

## 続きを読む

[チュートリアル ### Go（net/smtp）でSMTP経由でメールを送る 標準ライブラリのnet/smtpを使うか、net/httpでREST APIを呼び出します。MIMEヘッダー、HTMLメール、添付ファイル付き。 ガイドを読む](https://sendcorex.com/ja/blog/send-email-go) [基礎 ### SMTPとは？わかりやすいガイド SMTPは、インターネットで送られるほぼすべてのメールを運ぶプロトコルです。RFCの専門用語を使わずに、実際に何をしているのかをステップごとに解説します。 ガイドを読む](https://sendcorex.com/ja/blog/what-is-smtp) [到達率 ### SPF、DKIM、DMARCの解説 メールと受信箱の間には3つのDNSレコードがあります。それぞれが実際に何を証明するのか、1つ欠けると何が起こるのかを解説します。 ガイドを読む](https://sendcorex.com/ja/blog/spf-dkim-dmarc-explained)

数分で最初のメールを送信

無料アカウントを作成し、ドメインを認証して、上のコードを貼り付けましょう。

クレジットカード不要

[無料で始める — 月3,000通](https://app.sendcorex.com/authentication/register?appid=smtp) [料金を見る](https://sendcorex.com/ja/pricing)
