-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.go
98 lines (91 loc) · 2.23 KB
/
mail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"net/smtp"
"os"
"strings"
"time"
)
func SendToMail(user, password, host, to, subject, body, mialtype string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
var content_type string
if mialtype == "html" {
content_type = "Content-Type : text/" + mialtype + "; 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) {
resulut := make([]string, 0)
file, err := os.Open(filename)
//fmt.Println(file)
if err != nil {
return resulut, errors.New("打开文件失败")
}
defer file.Close()
bf := bufio.NewReader(file)
for {
line, isPrefix, err1 := bf.ReadLine()
if err1 != nil {
if err1 != io.EOF {
return resulut, errors.New("读行成功")
}
break
}
if isPrefix {
return resulut, errors.New("行太长")
}
str := string(line)
resulut = append(resulut, str)
}
return resulut, nil
}
func main() {
user := "[email protected]"
password := "XXX"//邮件SMTP授权密码
host := "smtp.qq.com:25"
//to := "[email protected]"
subject := "使用Golang发送邮件"
//body := "<html><body><h3>测试发送Email</h3></body></html>"
//fmt.Print("发送邮件")
// err := SendToMail(user, password, host, to, subject, body, "html")
// if err != nil {
// fmt.Println("发送邮件失败")
// fmt.Println(err)
// } else {
// fmt.Println("发送邮件成功")
// }
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 = SendToMail(user, password, host, to, subject, body, "html")
if err != nil {
fmt.Println("发送邮件失败")
fmt.Println(err)
i--
time.Sleep(600 * time.Second)
} else {
fmt.Println("发送邮件成功!")
}
}
}