用go的net/smtp来发邮件

版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权。 https://blog.csdn.net/stpeace/article/details/84144953

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

package main

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

func main() {
    senderEmail := "[email protected]"
    port := ":25"
    password := "yyyyyy"
    host := "smtp.gmail.com"
    auth := smtp.PlainAuth("", senderEmail, password, host)
    to := []string{"[email protected]"}
    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)
    }
}

        不多说。

猜你喜欢

转载自blog.csdn.net/stpeace/article/details/84144953