Problem
How do I set up outbound SMTP using the net/smtp package?
How do I use outMail in Go with net/smtp?
Solution
The following example of code assumes you have already got a fully functional development environment installed.
The below example of code shows you how to use outMail as the outgoing SMTP relay using the net/smtp package in Go (GoLang)
outMail is an authenticated SMTP relay so the example below shows authentication in perl as well. The code below uses the default SMTP port 25 but it can easily be changed to an alternative SMTP port by changing the addr: parameter.
Example code smtp-test.go.
package main
import (
"fmt"
"bytes"
"log"
"net/smtp"
)
func main() {
// Set the address information
from := "myaddress@example.com"
to := []string{
"sendingaddress@example.com",
}
// Set the SMTP Credentials
user := "outmail-username"
password := "outmail-password"
// Set the SMTP Server information the SMTP Port
addr := "mxXXXXXX.smtp-engine.com:25"
host := "mxXXXXXX.smtp-engine.com"
// Create the Message
msg := []byte("From: myaddress@example.com\r\n" +
"To: sendingaddress@example.com\r\n" +
"Subject: Test mail\r\n\r\n" +
"Email body\r\n")
// Create the Auth
auth := smtp.PlainAuth("", user, password, host)
// Send the message
err := smtp.SendMail(addr, auth, from, to, msg)
// Check for errors
if err != nil {
log.Fatal(err)
}
fmt.Println("Email sent successfully")
}
Summary of server details
Outgoing server |
mxXXXXXX.smtp-engine.com As provided in your signup email. |
Outgoing server protocol |
SMTP |
Outgoing server port |
25, 465, 587, 2525 or 8025 |
Authentication Type |
Basic Authentication, SSL and TLS supported |
Username |
As provided |
Password |
As provided |