How to Build an SMTP Client in Go

Web
Send emails in Go using the standard net/smtp package with a single SendMail function call.

Use the net/smtp package to connect to an SMTP server, authenticate, and send an email message.

package main

import (
	"fmt"
	"net/smtp"
)

func main() {
	from := "sender@example.com"
	password := "secret"
	to := []string{"recipient@example.com"}
	msg := []byte("Subject: Test\r\n\r\nThis is a test email.")

	auth := smtp.PlainAuth("", from, password, "smtp.example.com")
	err := smtp.SendMail("smtp.example.com:587", auth, from, to, msg)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Email sent successfully")
}