[Go] Golang: 简单smtp邮件发送样例 →→→→→进入此内容的聊天室

来自 , 2019-06-20, 写在 Go, 查看 127 次.
URL http://www.code666.cn/view/ec16c57e
  1. package main
  2.  
  3. import (
  4.         "log"
  5.         "net/smtp"
  6.         "flag"
  7.         "fmt"
  8.         "strings"
  9. )
  10.  
  11. var (
  12.     subject = flag.String( "s","","subject of the mail" )
  13.     body= flag.String( "b","","body of themail" )
  14.     reciMail = flag.String( "m","","recipient mail address" )
  15. )
  16.  
  17. func main() {
  18.         // Set up authentication information.
  19.         flag.Parse()
  20.         sub := fmt.Sprintf("subject: %s\r\n\r\n",*subject)
  21.         content :=  *body
  22.         mailList := strings.Split( *reciMail,",")
  23.  
  24.         auth := smtp.PlainAuth(
  25.                 "",
  26.                 "smtpuser@example.com",
  27.                 "password",
  28.                 "smtp.example.com",
  29.                 //"smtp.gmail.com",
  30.         )
  31.         // Connect to the server, authenticate, set the sender and recipient,
  32.         // and send the email all in one step.
  33.         err := smtp.SendMail(
  34.                 "smtp.example.com:25",
  35.                 auth,
  36.                 "senduser@example.com",
  37.                 mailList,
  38.                 []byte(sub+content),
  39.         )
  40.         if err != nil {
  41.                 log.Fatal(err)
  42.         }
  43. }
  44.  
  45.  
  46. //go/5641

回复 "Golang: 简单smtp邮件发送样例 "

这儿你可以回复上面这条便签

captcha