玩了一下, 发送邮件OK, 代码:

package main

import (
    "fmt"
    "net/smtp"
    "strings"
)

func main() {
    senderEmail := "xxx@gmail.com"
    port := ":25"
    password := "yyyyyy"
    host := "smtp.gmail.com"
    auth := smtp.PlainAuth("", senderEmail, password, host)
    to := []string{"zzz@gmail.com"}
    nickname := "sender"
    user := senderEmail

    subject := "this is title"
    content_type := "Content-Type: text/plain; charset=UTF-8"
    body := "this is content"
    msg := []byte("To: " + strings.Join(to, ",") + "\r\nFrom: " + nickname +
        "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
    err := smtp.SendMail(host + port, auth, user, to, msg)
    if err != nil {
        fmt.Printf("send mail error: %v", err)
    }
}

        不多说。

 


本文转载:CSDN博客