How to send emails in Golang

ai google send emails golang
A
Abhijeet Chattarjee

Email Design Specialist & Template Developer

 
June 30, 2025 3 min read

Introduction

net/smtp is a built-in go package and implements SMTP protocol. It provides a simple way for sending mail through smtp servers. We will explore the inbuilt net/smtp package and Gomail package to send out the emails.

Prerequisites

  • Basic understanding of Golang
  • Go version 1.10 or higher.

Send an Email with net/smtp

Importing packages
package main

import (
"fmt"
"net/smtp"
)

Sending the email
package main

import (
"fmt"
"net/smtp"
"os"
)

// Main function
func main() {
// from is sender's email address
from := os.Getenv("MAIL")
password := os.Getenv("PASSWD")

// toList is a list of email addresses that email is to be sent
toList := []string{"[email protected]"}

// host is address of server that the  sender's email address belongs,
// in this case it's gmail.
host := "smtp.gmail.com"

// It's the default port of smtp server
port := "587"

// This is the message to send in the mail
msg := "Hello World!"

// We can't send strings directly in mail,
// strings need to be converted into slice bytes
body := []byte(msg)

// PlainAuth uses the given username and password to
// authenticate to host and act as identity.
auth := smtp.PlainAuth("", from, password, host)

// SendMail uses TLS connection to send the mail
// The email is sent to all address in the toList,
// the body should be of type bytes, not strings
// This returns an error if any occurred.
err := smtp.SendMail(host+":"+port, auth, from, toList, body)

// handling the errors
if err != nil { 
    fmt.Println(err)
    os.Exit(1)
}

fmt.Println("Successfully sent email!")

}

Send an Email with Gomail

Importing package
package main

import (
"crypto/tls"
"fmt"
gomail "gopkg.in/mail.v2"
)

Sending the email
package main

import (
"crypto/tls"
"fmt"
gomail "gopkg.in/mail.v2"
)

func main() {
m := gomail.NewMessage()

m.SetHeader("From", "[email protected]")
m.SetHeader("To", "[email protected]")

m.SetHeader("Subject", "Sending Email with Goalang")

// Set the email body. You can set plain text or html with text/html
m.SetBody("text/plain", "Hello World!")

// Settings for SMTP server
d := gomail.NewDialer("smtp.gmail.com", 587, "[email protected]", "yourpassword")

// This is only needed when the SSL/TLS certificate is not valid on the server.
// In production this should be set to false.
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

if err := d.DialAndSend(m); err != nil {
    fmt.Println(err)
    panic(err)
}

return

}

Common Issues and Resolutions

  • Error: Invalid login: 535-5.7.8 Username and Password not accepted.
    A potential solution to this error could be to enable the Gmail service to use it in third-party apps. To resolve this error just login in Gmail account and enable less secure apps using this link https://myaccount.google.com/lesssecureapps

  • ETIMEDOUT errors
    Check your firewall settings. Timeout usually occurs when you try to open a connection to a port that is firewalled either on the server or on your machine.

  • TLS errors
    Check your antivirus settings. Antiviruses often mess around with email port usage. Node.js might not recognize the MITM cert your antivirus is using.
    Latest Node versions allow only TLS versions 1.2 and higher, some servers might still use TLS 1.1 or lower. Check Node.js docs how to get correct TLS support for your app.

Conclusion

Gmail either works well or it does not work at all. It is probably easier to switch to an alternative email service provider such as Mailazy instead of fixing issues with Gmail. Using gmail for your production server isn’t recommended either, you should opt for an email provider to manage your emails.
Configuring and setting up Mailazy is easy and seamless and emails can be sent in a few minutes!

A
Abhijeet Chattarjee

Email Design Specialist & Template Developer

 

A seasoned Product Marketing expert with extensive experience in product management and marketing strategies. Skilled in driving go-to-market plans, customer engagement, and cross-functional collaboration, they excel at aligning product development with market needs to deliver growth. Passionate about creating compelling product narratives, they combine data-driven insights with innovative solutions to boost brand visibility and customer satisfaction.

Related Articles

seo

6 Authentic Ways to Trace an IP Address from Email

Learn effective methods to trace IP addresses from emails for security verification, spam identification, and enhanced email management with practical step-by-step guidance.

By Abhijeet Chattarjee June 30, 2025 6 min read
Read full article
marketing

Demystifying Email Delivery: A Deep Dive into Email APIs, SMTP Servers, and Email Delivery Services

This blog post dives into the world of email APIs, SMTP servers, and email delivery services. Learn how they work together to get your emails into inboxes, not spam folders. Boost your email marketing results with this essential guide!

By Abhijeet Chattarjee June 30, 2025 7 min read
Read full article
content

21 SMTP Response Codes

The SMTP response code list can be used to help quickly determine the reason for email bounces or why you received an SMTP error when sending an email.

By Rahul Yadav June 30, 2025 6 min read
Read full article
digital

6 Tips for Defending Against Business Email Cybercriminal Attacks

Learn how to defend your firm from business email cybercriminal attacks, including how to spot and block them. Discover important tips with Mailazy now!

By Abhijeet Chattarjee June 30, 2025 6 min read
Read full article