這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
好久沒有更新部落格了,這次又帶來了一段GO語言的代碼(沒辦法,只會寫代碼了)。
Go語言郵件群發器 main.go
// Multi-SendEmail project main.gopackage mainimport ( "bufio" "errors" "fmt" "io" "io/ioutil" "net/smtp" "os" "strings" "time")func SendMail(user, password, host, to, subject, body, mailtype string) error { hp := strings.Split(host, ":") auth := smtp.PlainAuth("", user, password, hp[0]) var content_type string if mailtype == "html" { content_type = "Content-Type: text/html;charset=UTF-8" } else { content_type = "Content-Type: text/plain;charset=UTF-8" } msg := []byte("To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body) send_to := strings.Split(to, ";") err := smtp.SendMail(host, auth, user, send_to, msg) return err}func readLine2Array(filename string) ([]string, error) { result := make([]string, 0) file, err := os.Open(filename) if err != nil { return result, errors.New("Open file failed.") } defer file.Close() bf := bufio.NewReader(file) for { line, isPrefix, err1 := bf.ReadLine() if err1 != nil { if err1 != io.EOF { return result, errors.New("ReadLine no finish") } break } if isPrefix { return result, errors.New("Line is too long") } str := string(line) result = append(result, str) } return result, nil}func main() { fmt.Println("start...") user := "郵箱@qq.com" password := "密碼" host := "smtp.qq.com:25" //QQ為例 subject := "郵件標題" sendTo, err := readLine2Array("send.txt") if err != nil { fmt.Println(err) return } content, err := ioutil.ReadFile("email.txt") if err != nil { fmt.Println(err) return } body := string(content) for i := 0; i < len(sendTo); i++ { to := sendTo[i] fmt.Println("Send email to " + to) err = SendMail(user, password, host, to, subject, body, "html") if err != nil { fmt.Println("send mail error!") fmt.Println(err) i-- time.Sleep(600 * time.Second) } else { fmt.Println("send mail success!") } }}
使用說明:
編譯出exe檔案,把要發送的郵箱一行一個放入send.txt中,郵件內容放入email.txt中(HTML格式),運行。
這代碼我也有在用,有很多不靈活的地方,僅供參考。
============================================
剛開始學著寫一些部落格,寫得不好的地方請見諒,覺得學到了的就賞臉關注下吧~
我的QQ群(PHP)336533596,歡迎加入。相互交流,聊天吹牛~