smtp接口说明--使用go语言通过smtp协议发送邮件
package main
 
import (
    "fmt"
    "net/smtp"
    "strings"
)
 
func SendToMail(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 + "\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 := "U-MAIL创建的发件人地址"
    password := "U-MAIL后台设置的SMTP密码"
    host := "smtp.bestedm.org:25"
    to := "目标地址"
 
    subject := "test Golang to sendmail"
 
    body := `
        <html>
        <body>
        <h3>
        "Test send to email"
        </h3>
        </body>
        </html>
        `
    fmt.Println("send email")
    err := SendToMail(user, password, host, to, subject, body, "html")
    if err != nil {
        fmt.Println("Send mail error!")
        fmt.Println(err)
    } else {
        fmt.Println("Send mail success!")
    }
 
}
smtp接口说明--使用go语言通过smtp协议发送邮件.txt · 最后更改: 2017/03/22 03:52 (外部编辑)