阅读237 返回首页    go 手机大全


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 + "rnFrom: " + user + "rnSubject: " + subject + "rn" + content_type + "rnrn" + body)
    send_to := strings.Split(to, ";")
    err := smtp.SendMail(host, auth, user, send_to, msg)
    return err
}

func main() {
    user := "控制台创建的发信地址"
    password := "控制台设置的SMTP密码"
    host := "smtpdm.aliyun.com: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!")
    }

}

最后更新:2016-11-24 11:23:48

  上一篇:go SMTP之nodejs调用示例__SMTP接口说明_邮件推送-阿里云
  下一篇:go JavaSDK手册__SDK使用手册_邮件推送-阿里云