Sendcorex

การส่งถึงกล่องจดหมาย

การเข้ากล่องจดหมาย+62%

98.4%

Gmail
Outlook
Yahoo

อีเมลของคุณเข้ากล่องจดหมาย ไม่ใช่ถังสแปม

ตรวจติดตามการส่งถึงฟรีในทุกแพ็กเกจ

ดูเพิ่มเติม
บทเรียน

ส่งอีเมลด้วย .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

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

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

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