SoFunction
Updated on 2025-03-01

Golang implements the method of sending emails through SMTP

package main
import (
    "fmt"
    "net/mail"
    "net/smtp"
    "encoding/base64"
)
func main() {
    b64 := ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
    host := ""
    email := "mail1@"
    password := "password"
    toEmail := "mail2@"
from := {"Sender", email}
to := {"Recipient", toEmail}
    header := make(map[string]string)
    header["From"] = ()
    header["To"] = ()
header["Subject"] = ("=?UTF-8?B?%s?=", ([]byte("Mail Title 2")))
    header["MIME-Version"] = "1.0"
    header["Content-Type"] = "text/html; charset=UTF-8"
    header["Content-Transfer-Encoding"] = "base64"
body := "I'm an email! Golang sent it.";
    message := ""
    for k, v := range header {
        message += ("%s: %s\r\n", k, v)
    }
    message += "\r\n" + ([]byte(body))
    auth := (
        "",
        email,
        password,
        host,
    )
    err := (
        host+":25",
        auth,
        email,
        []string{},
        []byte(message),
    )
    if err != nil {
        panic(err)
    }
}