Create Gmail SMTP
Steps
Login to your google account -> Select App Passwords


Example Code
package main
import (
"fmt"
"log"
"net/smtp"
"gopkg.in/gomail.v2"
)
func main() {
// Gmail SMTP server details
smtpServer := "smtp.gmail.com"
smtpPort := 587 // Recommended port for TLS
senderEmail := "your_email@gmail.com" // Replace with your Gmail address
senderPassword := "your_app_password" // Replace with the App Password you generated in your Google Account.
recipientEmail := "to@example.com" // Replace with the recipient's email
subject := "Email via Gmail (using gomail v2)"
body := "This email is being sent through Gmail's SMTP server using Go and the gomail v2 library."
// Create a new gomail message
m := gomail.NewMessage()
m.SetHeader("From", senderEmail)
m.SetHeader("To", recipientEmail)
m.SetHeader("Subject", subject)
m.SetBody("text/plain", body)
// Create a new SMTP dialer
d := gomail.NewDialer(smtpServer, smtpPort, senderEmail, senderPassword)
// Send the email
if err := d.DialAndSend(m); err != nil {
log.Fatalf("Error sending email via Gmail (gomail v2): %v", err)
}
fmt.Println("Email sent successfully via Gmail using gomail v2!")
}
Last updated