Guide · Go, Rust & JVM
How to send email with Go
Send email from Go with net/http and encoding/json, using only the standard library.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
export STACKEMAILS_API_KEY=se_live_xxxxxxxxxxxx
2. Send your first email
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
)
func main() {
body, _ := json.Marshal(map[string]any{
"from": "Acme <hello@mail.acme.com>",
"to": "ada@example.com",
"subject": "Welcome to Acme",
"html": "<p>Thanks for signing up!</p>",
})
req, _ := http.NewRequest("POST", "https://stackemails.com/v1/emails", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("STACKEMAILS_API_KEY"))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: 10 * time.Second}
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
var out map[string]any
json.NewDecoder(res.Body).Decode(&out)
fmt.Println(res.StatusCode, out)
}
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
3. Or send over SMTP with net/smtp
package main
import (
"crypto/tls"
"net/smtp"
"os"
)
func main() {
host := "smtp.stackemails.com"
conn, err := tls.Dial("tcp", host+":465", &tls.Config{ServerName: host})
if err != nil {
panic(err)
}
c, _ := smtp.NewClient(conn, host)
defer c.Quit()
c.Auth(smtp.PlainAuth("", "stackemails", os.Getenv("STACKEMAILS_API_KEY"), host))
c.Mail("hello@mail.acme.com")
c.Rcpt("ada@example.com")
w, _ := c.Data()
w.Write([]byte("From: Acme <hello@mail.acme.com>\r\nTo: ada@example.com\r\nSubject: Welcome\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<p>Hello!</p>"))
w.Close()
}
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.