package main
import (
"fmt"
"net/smtp"
)
func main() {
// Sender email address and password
senderEmail := "[email protected]"
senderPassword := "your_email_password"
// Recipient email address
recipientEmail := "[email protected]"
// SMTP server configuration
smtpHost := "smtp.example.com"
smtpPort := "587"
// Compose the email message
message := []byte("Subject: Your Subject\r\n" +
"\r\n" +
"Hello,\r\n" +
"This is the body of the email.\r\n")
// Authentication information
auth := smtp.PlainAuth("", senderEmail, senderPassword, smtpHost)
// Connect to the SMTP server
client, err := smtp.Dial(smtpHost + ":" + smtpPort)
if err != nil {
fmt.Println("Error connecting to the SMTP server:", err)
return
}
defer client.Close()
// Authenticate with the SMTP server
err = client.Auth(auth)
if err != nil {
fmt.Println("Error authenticating with the SMTP server:", err)
return
}
// Set the sender and recipient
err = client.Mail(senderEmail)
if err != nil {
fmt.Println("Error setting sender:", err)
return
}
err = client.Rcpt(recipientEmail)
if err != nil {
fmt.Println("Error setting recipient:", err)
return
}
// Send the email message
w, err := client.Data()
if err != nil {
fmt.Println("Error preparing to send data:", err)
return
}
_, err = w.Write(message)
if err != nil {
fmt.Println("Error writing message body:", err)
return
}
err = w.Close()
if err != nil {
fmt.Println("Error closing writer:", err)
return
}
fmt.Println("Email sent successfully.")
}