---
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-go

บทเรียน

# ส่งอีเมลด้วย Go (net/smtp) ผ่าน SMTP

ไลบรารีมาตรฐานก็พอ ไม่ต้องใช้ dependency ภายนอก

โดยทีม Sendcorex·อัปเดต 21 ก.ย. 2026·อ่าน 7 นาที

## ตัวเลือกที่ 1: net/smtp

ไลบรารีมาตรฐานของ Go มีทุกอย่างที่ต้องใช้ อีเมล HTML ต้องเขียน header MIME content-type ลงในเนื้อหาข้อความเอง:

```
package main

import (
    "net/smtp"
    "os"
)

func main() {
    apiKey := os.Getenv("SENDCOREX_API_KEY")
    auth := smtp.PlainAuth("", apiKey, apiKey, "smtp.sendcorex.com")

    to := []string{"customer@example.com"}
    msg := []byte("To: customer@example.com\r\n" +
        "From: Your App <hello@yourdomain.com>\r\n" +
        "Subject: Welcome!\r\n" +
        "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
        "<h1>You are in.</h1>")

    err := smtp.SendMail("smtp.sendcorex.com:587", auth, "hello@yourdomain.com", to, msg)
    if err != nil {
        panic(err)
    }
}
```

## ตัวเลือกที่ 2: REST API ด้วย net/http

```
body := []byte(`{"to":"customer@example.com","from":"hello@yourdomain.com","subject":"Welcome!","html":"<h1>You are in.</h1>"}`)

req, _ := http.NewRequest("POST", "https://graph.sendcorex.com/v3.0/emails", bytes.NewBuffer(body))
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
```

## ข้อผิดพลาดที่พบบ่อย

| อาการ | วิธีแก้ |
| --- | --- |
| `535 Authentication failed` | ตรวจว่า API key ถูกต้องและใช้ในทั้งสองช่องของ `PlainAuth` |
| ข้อความแสดงเป็นข้อความธรรมดา | header `Content-Type: text/html` ต้องอยู่ในข้อความดิบ ก่อนบรรทัดว่าง |

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

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

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

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

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

## อ่านต่อ

[บทเรียน ### ส่งอีเมลด้วย .NET (C#) ผ่าน SMTP ใช้ System.Net.Mail หรือ MailKit ผ่าน SMTP พร้อมอีเมล HTML ไฟล์แนบ การจัดการข้อผิดพลาด และการตั้งค่า DNS อ่านคู่มือ](https://sendcorex.com/th/blog/send-email-dotnet) [พื้นฐาน ### 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)
