---
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/th/blog/send-email-dotnet

บทเรียน

# ส่งอีเมลด้วย .NET (C#) ผ่าน SMTP

ใช้ไคลเอนต์ System.Net.Mail ในตัว หรือ MailKit เมื่อต้องการ TLS สมัยใหม่ async และการรายงานข้อผิดพลาดที่เชื่อถือได้

โดยทีม Sendcorex·อัปเดต 21 ก.ย. 2026·อ่าน 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 ต้องใช้ `SslOnConnect` ไม่ใช่ `StartTls` |

### ในหน้านี้

ส่งอีเมลแรกในไม่กี่นาที

สร้างบัญชีฟรี ยืนยันโดเมน แล้ววางโค้ดด้านบน

[เริ่มฟรี — 3,000 อีเมล/เดือน](https://app.sendcorex.com/authentication/register?appid=smtp)

ไม่ต้องใช้บัตรเครดิต

## อ่านต่อ

[บทเรียน ### ส่งอีเมลด้วย Go (net/smtp) ผ่าน SMTP ใช้ net/smtp จากไลบรารีมาตรฐาน หรือเรียก REST API ด้วย net/http พร้อม header MIME อีเมล HTML และไฟล์แนบ อ่านคู่มือ](https://sendcorex.com/th/blog/send-email-go) [พื้นฐาน ### SMTP คืออะไร? คู่มือภาษาง่ายๆ SMTP คือโปรโตคอลที่ส่งอีเมลเกือบทุกฉบับบนอินเทอร์เน็ต นี่คือสิ่งที่มันทำจริงๆ ทีละขั้นตอน โดยไม่ใช้ศัพท์เทคนิคของ RFC อ่านคู่มือ](https://sendcorex.com/th/blog/what-is-smtp) [การส่งถึง ### อธิบาย SPF, DKIM และ DMARC ระเบียน DNS สามตัวคั่นระหว่างอีเมลของคุณกับกล่องจดหมาย นี่คือสิ่งที่แต่ละตัวพิสูจน์จริงๆ และสิ่งที่เกิดขึ้นเมื่อขาดตัวใดตัวหนึ่ง อ่านคู่มือ](https://sendcorex.com/th/blog/spf-dkim-dmarc-explained)

ส่งอีเมลแรกในไม่กี่นาที

สร้างบัญชีฟรี ยืนยันโดเมน แล้ววางโค้ดด้านบน

ไม่ต้องใช้บัตรเครดิต

[เริ่มฟรี — 3,000 อีเมล/เดือน](https://app.sendcorex.com/authentication/register?appid=smtp) [ดูราคา](https://sendcorex.com/th/pricing)
