---
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/vi/blog/send-email-go

Bài thực hành

# Gửi email bằng Go (net/smtp) qua SMTP

Thư viện chuẩn là đủ — không cần thư viện bên thứ ba.

Bởi đội ngũ Sendcorex·Cập nhật 21 thg 9, 2026·7 phút đọc

## Cách 1: net/smtp

Thư viện chuẩn của Go có mọi thứ bạn cần. Thư HTML cần header MIME content-type viết thủ công vào nội dung tin nhắn:

```
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)
    }
}
```

## Cách 2: REST API với 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)
```

## Lỗi thường gặp

| Triệu chứng | Cách sửa |
| --- | --- |
| `535 Authentication failed` | Xác nhận API key đúng và được dùng cho cả hai trường trong `PlainAuth`. |
| Tin nhắn hiển thị dạng văn bản thuần | Header `Content-Type: text/html` phải nằm trong tin nhắn thô, trước dòng trống. |

### Trên trang này

Gửi email đầu tiên trong vài phút

Tạo tài khoản miễn phí, xác minh tên miền và dán code ở trên.

[Bắt đầu miễn phí — 3.000 email/tháng](https://app.sendcorex.com/authentication/register?appid=smtp)

Không cần thẻ tín dụng

## Đọc tiếp

[Bài thực hành ### Gửi email bằng .NET (C#) qua SMTP Dùng System.Net.Mail hoặc MailKit qua SMTP — kèm email HTML, tệp đính kèm, xử lý lỗi và cấu hình DNS. Đọc hướng dẫn](https://sendcorex.com/vi/blog/send-email-dotnet) [Kiến thức cơ bản ### SMTP là gì? Hướng dẫn dễ hiểu SMTP là giao thức chuyển gần như mọi email gửi trên internet. Đây là những gì nó thực sự làm, từng bước — không dùng thuật ngữ RFC. Đọc hướng dẫn](https://sendcorex.com/vi/blog/what-is-smtp) [Khả năng vào hộp thư ### Giải thích SPF, DKIM và DMARC Ba bản ghi DNS đứng giữa email của bạn và hộp thư. Đây là những gì mỗi bản ghi thực sự chứng minh, và điều gì xảy ra khi thiếu một cái. Đọc hướng dẫn](https://sendcorex.com/vi/blog/spf-dkim-dmarc-explained)

Gửi email đầu tiên trong vài phút

Tạo tài khoản miễn phí, xác minh tên miền và dán code ở trên.

Không cần thẻ tín dụng

[Bắt đầu miễn phí — 3.000 email/tháng](https://app.sendcorex.com/authentication/register?appid=smtp) [Xem bảng giá](https://sendcorex.com/vi/pricing)
