go语言发送邮件

package main

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

//发送邮件的逻辑函数
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 + "
From: " + user + "<" + user + ">
Subject: " + subject + "
" + content_type + "

" + body)
	send_to := strings.Split(to, ";")
	err := smtp.SendMail(host, auth, user, send_to, msg)
	return err
}

func main() {
	// 邮箱账号
	user := "xxxx@163.com"
	//注意,此处为授权码、不是密码
	password := "xxxx"
	//smtp地址及端口
	host := "smtp.163.com:25"
	//接收者,内容可重复,多个邮箱之间用;隔开
	to := "xxxxxx@qq.com"
	//邮件主题
	subject := "测试通过golang发送邮件"
	//邮件内容
	text := "XXX你好!"
	body := `
    <html>
    <body>
    <h3>
    "测试通过golang发送邮件"` + text + `
    </h3>
    </body>
    </html>
    `
	//
	fmt.Println("send email")
	//执行逻辑函数
	err := SendMail(user, password, host, to, subject, body, "html")
	if err != nil {
		fmt.Println("发送邮件失败!")
		fmt.Println(err)
	} else {
		fmt.Println("发送邮件成功!")
	}

}

原文地址:https://www.cnblogs.com/zheng-chuang/p/6058950.html