mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:08:53 +08:00
31 lines
738 B
Go
31 lines
738 B
Go
// smtp.go
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"log"
|
|
"net/smtp"
|
|
)
|
|
|
|
func main() {
|
|
// Connect to the remote SMTP server.
|
|
client, err := smtp.Dial("mail.example.com:25")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
// Set the sender and recipient.
|
|
client.Mail("sender@example.org")
|
|
client.Rcpt("recipient@example.net")
|
|
// Send the email body.
|
|
wc, err := client.Data()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer wc.Close()
|
|
buf := bytes.NewBufferString("This is the email body.")
|
|
if _, err = buf.WriteTo(wc); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|