Junior — Middle
Kas teil on kogemusi SMTP protokolli kasutamisel e-kirjade saatmiseks?
sobes.tech AI
Vastus AI-lt
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 message.
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)
}
}
In this example, a connection to the SMTP server is established with authentication, and the email is sent. TLS can be used for security, and headers and attachments can also be added to the email.