7.10 smtp send email


package main

import (
	"crypto/tls"
	"fmt"
	"net/smtp"
)

func main() {

	var email string
	fmt.Println("Enter username for smtp: ")
	fmt.Scanln(&email)

	var pass string
	fmt.Println("Enter password for smtp: ")
	fmt.Scanln(&pass)

	auth := smtp.PlainAuth("",
		email,
		pass,
		"smtp.gmail.com")

	c, err := smtp.Dial("smtp.gmail.com:587")
	if err != nil {
		panic(err)
	}
	defer c.Close()
	config := &tls.Config{ServerName: "smtp.gmail.com"}

	if err = c.StartTLS(config); err != nil {
		panic(err)
	}

	if err = c.Auth(auth); err != nil {
		panic(err)
	}

	if err = c.Mail(email); err != nil {
		panic(err)
	}
	if err = c.Rcpt(email); err != nil {
		panic(err)
	}

	w, err := c.Data()
	if err != nil {
		panic(err)
	}

	msg := []byte("Hello this is content")
	if _, err := w.Write(msg); err != nil {
		panic(err)
	}

	err = w.Close()
	if err != nil {
		panic(err)
	}
	err = c.Quit()

	if err != nil {
		panic(err)
	}

}

/*


*/

posted on 2018-03-24 00:03  cucy_to  阅读(135)  评论(0编辑  收藏  举报

导航