用golang发送邮件

用golang发送邮件
配置文件 conf.json

{
"Username":	"sunxxg@163.com",
"Password":	"123456",
"Smtphost":"smtp.163.com:25"
}

主程序  sendmail.go

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/smtp"
	"os"
	"strings"
)

type cfgmail struct {
	Username string
	Password string
	Smtphost string
}

type cfg struct {
	Name, Text string
}

func main() {

//从json文件中读取发送邮件服务器配置信息
	cfgjson := getConf()

	var cfg cfgmail
	dec := json.NewDecoder(strings.NewReader(cfgjson))
	for {

		if err := dec.Decode(&cfg); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}

		//fmt.Printf("%s\n%s\n%s\n", cfg.Username, cfg.Password, cfg.Smtphost)

	}

	username := cfg.Username
	password := cfg.Password
	host := cfg.Smtphost

	to := "10010@qq.com"

	fmt.Printf("============")
	fmt.Println(username)
	subject := "能否收到邮件哟?Test send email by golang"

	body := `
	<html>
	<body>
	<h3>
	"Test send email by golang,来个测试试一下"
	</h3>
	</body>
	</html>
	`

	err := SendMail(username, password, host, to, subject, body, "html")
	if err != nil {
		fmt.Println("send mail error!")
		fmt.Println(err)
	} else {
		fmt.Println("send mail success!")
	}

}

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 + "\r\nFrom: " + user + "<" + 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 getConf() string {
	filename := "conf.json"
	file, err := os.Open(filename)

	defer file.Close()
	if err != nil {
		fmt.Println("read conf file error")
		log.Fatal(err)
	}

	buf := make([]byte, 512)
var str1 string
	for {
		n, _ := file.Read(buf)
		if 0 == n {
			break
		}
		//os.Stdout.Write(buf[:n])

		str := string(buf[:n])

		str1 = str1 + str
	}
	return str1
}

然后执行

go run sendmail.go

即可.

对于一些文件的操作,可参考:https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.0.md

43 thoughts on “用golang发送邮件

  1. Pingback: แทงบอล77

  2. Pingback: dang ky tai khoan w88

  3. Pingback: 우리계열카지노

  4. Pingback: homes for sale in Placerville

  5. Pingback: blog link

  6. Pingback: 바카라사이트

  7. Pingback: 카지노사이트

  8. Pingback: con heo dat

  9. Pingback: ซี ฉัตรปวีณ์ ตรีชัชวาลวงศ์

  10. Pingback: ウォーターサーバー

  11. Pingback: link12bet

  12. Pingback: replica gerald genta watch watches

  13. Pingback: i99casino

  14. Pingback: industrial floor stickers

  15. Pingback: cbd oil

  16. Pingback: porn

  17. Pingback: knockoff best tag heuer

  18. Pingback: application

  19. Pingback: เงินด่วน

  20. Pingback: KIU-Library/

  21. Pingback: KIU

  22. Pingback: Online Shopping

  23. Pingback: google

  24. Pingback: CISO

  25. Pingback: yourfishguide.com mahi mahi fish

  26. Pingback: 오피쓰

  27. Pingback: Exotic Animals for Sale

  28. Pingback: เงินนอกระบบ ได้จริง 2019

  29. Pingback: Tattoo Supplies

  30. Pingback: 먹튀폴리스

  31. Pingback: English bulldog puppies for sale near me in CA ON MA CO OH PA SC MS TN FL UT NH VA AL TX

  32. Pingback: dang ky 188bet

  33. Pingback: 63.250.38.81

  34. Pingback: cheap wigs

  35. Pingback: Idgod

  36. Pingback: cbd oil dosage calculator

  37. Pingback: buy/order adderall xr 20mg 30mg online pharmacy legally no script for ADHD, anxiety, depression in USA UK Canada Australia Europe overnight delivery

  38. Pingback: buy pills erection viagra

  39. Pingback: viagra without a doctor prescription

  40. Pingback: bitcoin era review 2020

  41. Pingback: bitcoin evolution review

  42. Pingback: immediate edge review

  43. Pingback: cialistodo.com

Comments are closed.