smtp.go 738 B

123456789101112131415161718192021222324252627282930
  1. // smtp.go
  2. package main
  3. import (
  4. "bytes"
  5. "log"
  6. "net/smtp"
  7. )
  8. func main() {
  9. // Connect to the remote SMTP server.
  10. client, err := smtp.Dial("mail.example.com:25")
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. // Set the sender and recipient.
  15. client.Mail("[email protected]")
  16. client.Rcpt("[email protected]")
  17. // Send the email body.
  18. wc, err := client.Data()
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. defer wc.Close()
  23. buf := bytes.NewBufferString("This is the email body.")
  24. if _, err = buf.WriteTo(wc); err != nil {
  25. log.Fatal(err)
  26. }
  27. }