【go语言发送电子邮件】go语言版发送电子邮件
来源:互联网 发布:大庆龙江网络新村电话 编辑:程序博客网 时间:2024/05/22 23:51
一、实现功能
用go语言发送一封邮件
二、实现源代码
package mainimport ( "net/smtp" "fmt" "strings")/* * user : example@example.com login smtp server user * password: xxxxx login smtp server password * host: smtp.example.com:port smtp.163.com:25 * to: example@example.com;example1@163.com;example2@sina.com.cn;... * subject:The subject of mail * body: The content of mail * mailtyoe: mail type html or text */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/"+ mailtype + "; 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 main() { user := "此处填写qq邮箱账号" password := "此处填写qq邮箱授权码" host := "smtp.qq.com:587" to := "18720081236m@sina.cn" subject := "Test send email by golang" body := `` fmt.Println("send email") err := SendMail(user, password, host, to, subject, body, "text") if err != nil { fmt.Println("send mail error!") fmt.Println(err) }else{ fmt.Println("send mail success!") }}
运行效果
"D:\Program Files (x86)\JetBrains\Gogland 171.3780.106\bin\runnerw.exe" D:/Go\bin\go.exe run D:/Go/code/src/awesomeProject/go_email.gosend emailsend mail success!Process finished with exit code 0
版本2(发送多个人 ,HTML格式):
package mainimport ( "net/smtp" "fmt" "strings")/* * user : example@example.com login smtp server user * password: xxxxx login smtp server password * host: smtp.example.com:port smtp.163.com:25 * to: example@example.com;example1@163.com;example2@sina.com.cn;... * subject:The subject of mail * body: The content of mail * mailtyoe: mail type html or text */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/"+ mailtype + "; 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 main() { user := "1973536419@qq.com" password := "XXXXXXXX" host := "smtp.qq.com:587" to := "defa.lai@cgtz.com;fang.chen@lg-finance.com"; subject := "Test send email by golang" body := ` <!DOCTYPE html> <html> <body> <div style="text-align:center;width:78.78%;padding: 8px; line-height: 1.42857; vertical-align: top; border-top-width: 1px; border-top-color: rgb(221, 221, 221); background-color: #28a745;color:#fff"><strong>用户工资数据报表</strong></div> <table border="1" style="width:80%;"> <tr style="background-color:pink;text-align:center;"> <th>月份</th> <th>存款</th> <th>工资</th> <th>年薪</th> </tr> <tr style="text-align:center;"> <td>一月</td> <td>1000 元</td> <td>3000 元</td> <td>12000元</td> </tr> <tr style="text-align:center;"> <td>二月</td> <td>1500 元</td> <td>4000 元</td> <td>16000 元</td> </tr> </table> </body> </html> ` fmt.Println("send email") err := SendMail(user, password, host, to, subject, body, "html") if err != nil { fmt.Println("send mail error!") fmt.Println(err) }else{ fmt.Println("send mail success!") }}
阅读全文
1 0
- 【go语言发送电子邮件】go语言版发送电子邮件
- C语言发送电子邮件
- C语言发送电子邮件
- 发送电子邮件
- 发送电子邮件
- 发送电子邮件
- 发送电子邮件
- 发送电子邮件
- 发送电子邮件
- 发送电子邮件
- 电子邮件发送
- 发送电子邮件
- 发送电子邮件
- 发送电子邮件
- 电子邮件发送
- 发送电子邮件
- 发送电子邮件
- 简易电子邮件发送程序!
- hive Caused by: java.io.EOFException: Unexpected end of input stream排查
- pycharme2017注册码
- Maven Prepare a Release
- eclipse中集成tomcat8
- zlog日志函数库
- 【go语言发送电子邮件】go语言版发送电子邮件
- linux SD启动卡分区详解
- u3d抗锯齿配置
- Hibernate_day02
- Eclipse修改皮肤主题、编辑区、Log区背景
- Java学习笔记-《Java程序员面试宝典》-第四章基础知识-4.1基本概念(4.1.2)
- MyBatis(四) sql执行流程
- 打开mac自身支持ntfs的读写
- mongodb