Junior — Middle
Have you worked with SMTP protocol for sending emails?
sobes.tech AI
Answer from AI
In Go, the net/smtp package is often used for sending emails via SMTP. It allows establishing a connection with the SMTP server, authenticating, and sending the email.
Example of sending a simple email:
package main
import (
"net/smtp"
"log"
)
func main() {
from := "sender@example.com"
password := "password"
to := []string{"recipient@example.com"}
smtpHost := "smtp.example.com"
smtpPort := "587"
message := []byte("Subject: Test Email\r\n" +
"\r\n" +
"This is the body of the email.")
auth := smtp.PlainAuth("", from, password, smtpHost)
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message)
if err != nil {
log.Fatal(err)
}
}
This example demonstrates connecting to an SMTP server with authentication and sending an email. TLS can be used for security, and the email can be extended with headers and attachments.