最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

go?smtp實(shí)現(xiàn)郵件發(fā)送示例詳解

 更新時(shí)間:2022年05月19日 15:44:09   作者:dz45693  
這篇文章主要為大家介紹了go?smtp實(shí)現(xiàn)郵件發(fā)送示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

smtp指令

書接上文郵件實(shí)現(xiàn)詳解,這里我們及我們簡單復(fù)習(xí)一下smtp的指令如下:

telnet smtp.163.com 25
[outpout]
ehlo dz45693
[outpout]
auth login
[outpout]
輸入用戶名base64
[outpout]
輸入密碼base64
mail from:<dz45693@163.com>
[outpout]
rcpt to:<dz45693@sina.com>
[outpout]
data
[outpout]
from:<dz45693@163.com>
to:<dz45693@sina.com>
subject:hello world
This is the first email sent by hand using the SMTP protocol
quit  

go demo

好,那我們下現(xiàn)在用go實(shí)現(xiàn)代碼讓如下:這里只是一個(gè)demo,主要熟悉smtp命令

package main
import (
	"bufio"
	"encoding/base64"
	"fmt"
	"net"
	"strconv"
	"strings"
)
func main() {
	testSmtp()
}
var gConn net.Conn
var gRead *bufio.Reader
var gWrite *bufio.Writer
//可以放到這樣的類里
type TcpClient struct {
	Conn  net.Conn
	Read  *bufio.Reader
	Write *bufio.Writer
} //
func Connect(host string, port int) (net.Conn, *bufio.Reader, *bufio.Writer) {
	addr := host + ":" + strconv.Itoa(port)
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, nil, nil
	}
	reader := bufio.NewReader(conn)
	writer := bufio.NewWriter(conn)
	return conn, reader, writer
} //
//收取一行,可再優(yōu)化
func RecvLine() string {
	line, err := gRead.ReadString('\n') //如何設(shè)定超時(shí)?
	if err != nil {
		fmt.Print(err)
		return ""
	}
	line = strings.Split(line, "\r")[0] //還要再去掉 "\r",其實(shí)不去掉也可以
	return line
}
func SendLine(line string) {
	gWrite.WriteString(line + "\r\n")
	gWrite.Flush()
}
//解碼一行命令,這里比較簡單就是按空格進(jìn)行分隔就行了
func DecodeCmd(line string, sp string) []string {
	tmp := strings.Split(line, sp)
	var cmds = []string{"", "", "", "", ""} //先定義多幾個(gè),以面后面使用時(shí)產(chǎn)生異常
	for i := 0; i < len(tmp); i++ {
		if i >= len(cmds) {
			break
		}
		cmds[i] = tmp[i]
	}
	return cmds
}
//讀取多行結(jié)果
func RecvMCmd() string {
	i := 0
	rs := ""
	mLine := ""
	for i = 0; i < 50; i++ {
		rs = RecvLine() //只收取一行
		mLine = mLine + rs + "\r\n"
		if len(rs) < 4 {
			break
		} //長度要足夠
		c4 := rs[4-1] //第4個(gè)字符
		if ' ' == c4 {
			break
		} //第4個(gè)字符是空格就表示讀取完了//也可以判斷 "250[空格]"
	}
	return mLine
}
//簡單的測試一下 smtp
func testSmtp() {
	//連接
	gConn, gRead, gWrite = Connect("smtp.163.com", 25)
	defer gConn.Close()
	//收取一行
	line := RecvLine()
	fmt.Println("recv:" + line)
	//解碼一下,這樣后面的 EHLO 才能有正確的第二個(gè)參數(shù)
	cmds := DecodeCmd(line, " ")
	domain := cmds[1] //要從對方的應(yīng)答中取出域名//空格分開的各個(gè)命令參數(shù)中的第二個(gè)
	//發(fā)送一個(gè)命令
	SendLine("EHLO" + " " + domain) //domain 要求其實(shí)來自 HELO 命令//HELO <SP> <domain> <CRLF>
	//收取多行
	line = RecvMCmd()
	fmt.Println("recv:" + line)
	//--------------------------------------------------
	//用 base64 登錄
	SendLine("AUTH LOGIN")
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	s := "dz45693" //要換成你的用戶名,注意 163 郵箱的話不要帶后面的 @域名 部分
	s = base64.StdEncoding.EncodeToString([]byte(s))
	SendLine(s)
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	s = "xxxxx" //要換成您的密碼
	s = base64.StdEncoding.EncodeToString([]byte(s))
	SendLine(s)
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	//--------------------------------------------------
	//郵件內(nèi)容
	from := "dz45693@163.com"
	to := "dz45693@sina.com"
	SendLine("MAIL FROM: <" + from + ">") //注意"<" 符號和前面的空格??崭裨趨f(xié)議中有和沒有都可能,最好還是有
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	SendLine("RCPT TO: <" + to + ">")
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	SendLine("DATA")
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
//發(fā)送郵件頭
	SendLine("from:<dz45693@163.com>")
	SendLine("to:<dz45693@sina.com>")
	SendLine("subject:hello world")
	SendLine("") //發(fā)送空行 后面就是郵件體
	SendLine("This is the first email sent by hand using the SMTP protocol")
	SendLine(".") //郵件結(jié)束符
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	SendLine("quit") //鏈接推出
	line = RecvLine()
	fmt.Println("recv:" + line)
} //

運(yùn)行結(jié)果如下:

sdk中SendMail方法

在go的sdk中提供了SendMail方法【發(fā)送郵件后這個(gè)方法會關(guān)閉鏈接】,實(shí)現(xiàn)如下:

 實(shí)現(xiàn)如下:

 func SendMailBySmtp(){
	auth := smtp.PlainAuth("", "dz45693@163.com", "xxx", "smtp.163.com")
	to := []string{"dz45693@sina.com"}
	image,_:=ioutil.ReadFile("d:\\Downloads\\1.png")
	imageBase64:=base64.StdEncoding.EncodeToString(image)
	msg := []byte("from:dz45693@163.com\r\n"+
		"to: dz45693@sina.com\r\n" +
		"Subject: hello,subject!\r\n"+
		"Content-Type:multipart/mixed;boundary=a\r\n"+
		"Mime-Version:1.0\r\n"+
		"\r\n" +
		"--a\r\n"+
		"Content-type:text/plain;charset=utf-8\r\n"+
		"Content-Transfer-Encoding:quoted-printable\r\n"+
		"\r\n"+
		"此處為正文內(nèi)容!\r\n"+
		"--a\r\n"+
		"Content-type:image/jpg;name=1.jpg\r\n"+
		"Content-Transfer-Encoding:base64\r\n"+
		"\r\n"+
		imageBase64+"\r\n"+
		"--a--\r\n")
	err := smtp.SendMail("smtp.163.com:25", auth, "dz45693@163.com", to, msg)
	if err != nil {
		fmt.Println(err)
	}
}

運(yùn)行效果:

 使用第三方庫gomail實(shí)現(xiàn)郵件的發(fā)送更多了解,

請前往:https://pkg.go.dev/gopkg.in/gomail.v2?utm_source=godoc

示例如下:

func SendMailByGomailOne(){
	m := gomail.NewMessage()
	m.SetAddressHeader("From", "dz45693@163.com", "dz45693")
	m.SetHeader("To", "dz45693@sina.com")
	m.SetHeader("Subject", "hello SendMailByGomailOne!")
	m.Embed("d:\\Downloads\\1.png")
	m.SetBody("text/html", "此處為正文121333!")
	d := gomail.NewDialer("smtp.163.com", 25, "dz45693@163.com", "xxxx")
	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}
}

運(yùn)行結(jié)果:

DialAndSend實(shí)現(xiàn)

來我們看看DialAndSend的實(shí)現(xiàn)如下: 

package gomail
import (
	"crypto/tls"
	"fmt"
	"io"
	"net"
	"net/smtp"
	"strings"
	"time"
)
// A Dialer is a dialer to an SMTP server.
type Dialer struct {
	// Host represents the host of the SMTP server.
	Host string
	// Port represents the port of the SMTP server.
	Port int
	// Username is the username to use to authenticate to the SMTP server.
	Username string
	// Password is the password to use to authenticate to the SMTP server.
	Password string
	// Auth represents the authentication mechanism used to authenticate to the
	// SMTP server.
	Auth smtp.Auth
	// SSL defines whether an SSL connection is used. It should be false in
	// most cases since the authentication mechanism should use the STARTTLS
	// extension instead.
	SSL bool
	// TSLConfig represents the TLS configuration used for the TLS (when the
	// STARTTLS extension is used) or SSL connection.
	TLSConfig *tls.Config
	// LocalName is the hostname sent to the SMTP server with the HELO command.
	// By default, "localhost" is sent.
	LocalName string
}
// NewDialer returns a new SMTP Dialer. The given parameters are used to connect
// to the SMTP server.
func NewDialer(host string, port int, username, password string) *Dialer {
	return &Dialer{
		Host:     host,
		Port:     port,
		Username: username,
		Password: password,
		SSL:      port == 465,
	}
}
// NewPlainDialer returns a new SMTP Dialer. The given parameters are used to
// connect to the SMTP server.
//
// Deprecated: Use NewDialer instead.
func NewPlainDialer(host string, port int, username, password string) *Dialer {
	return NewDialer(host, port, username, password)
}
// Dial dials and authenticates to an SMTP server. The returned SendCloser
// should be closed when done using it.
func (d *Dialer) Dial() (SendCloser, error) {
	conn, err := netDialTimeout("tcp", addr(d.Host, d.Port), 10*time.Second)
	if err != nil {
		return nil, err
	}
	if d.SSL {
		conn = tlsClient(conn, d.tlsConfig())
	}
	c, err := smtpNewClient(conn, d.Host)
	if err != nil {
		return nil, err
	}
	if d.LocalName != "" {
		if err := c.Hello(d.LocalName); err != nil {
			return nil, err
		}
	}
	if !d.SSL {
		if ok, _ := c.Extension("STARTTLS"); ok {
			if err := c.StartTLS(d.tlsConfig()); err != nil {
				c.Close()
				return nil, err
			}
		}
	}
	if d.Auth == nil && d.Username != "" {
		if ok, auths := c.Extension("AUTH"); ok {
			if strings.Contains(auths, "CRAM-MD5") {
				d.Auth = smtp.CRAMMD5Auth(d.Username, d.Password)
			} else if strings.Contains(auths, "LOGIN") &&
				!strings.Contains(auths, "PLAIN") {
				d.Auth = &loginAuth{
					username: d.Username,
					password: d.Password,
					host:     d.Host,
				}
			} else {
				d.Auth = smtp.PlainAuth("", d.Username, d.Password, d.Host)
			}
		}
	}
	if d.Auth != nil {
		if err = c.Auth(d.Auth); err != nil {
			c.Close()
			return nil, err
		}
	}
	return &smtpSender{c, d}, nil
}
func (d *Dialer) tlsConfig() *tls.Config {
	if d.TLSConfig == nil {
		return &tls.Config{ServerName: d.Host}
	}
	return d.TLSConfig
}
func addr(host string, port int) string {
	return fmt.Sprintf("%s:%d", host, port)
}
// DialAndSend opens a connection to the SMTP server, sends the given emails and
// closes the connection.
func (d *Dialer) DialAndSend(m ...*Message) error {
	s, err := d.Dial()
	if err != nil {
		return err
	}
	defer s.Close()
	return Send(s, m...)
}
type smtpSender struct {
	smtpClient
	d *Dialer
}
func (c *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
	if err := c.Mail(from); err != nil {
		if err == io.EOF {
			// This is probably due to a timeout, so reconnect and try again.
			sc, derr := c.d.Dial()
			if derr == nil {
				if s, ok := sc.(*smtpSender); ok {
					*c = *s
					return c.Send(from, to, msg)
				}
			}
		}
		return err
	}
	for _, addr := range to {
		if err := c.Rcpt(addr); err != nil {
			return err
		}
	}
	w, err := c.Data()
	if err != nil {
		return err
	}
	if _, err = msg.WriteTo(w); err != nil {
		w.Close()
		return err
	}
	return w.Close()
}
func (c *smtpSender) Close() error {
	return c.Quit()
}
// Stubbed out for tests.
var (
	netDialTimeout = net.DialTimeout
	tlsClient      = tls.Client
	smtpNewClient  = func(conn net.Conn, host string) (smtpClient, error) {
		return smtp.NewClient(conn, host)
	}
)
type smtpClient interface {
	Hello(string) error
	Extension(string) (bool, string)
	StartTLS(*tls.Config) error
	Auth(smtp.Auth) error
	Mail(string) error
	Rcpt(string) error
	Data() (io.WriteCloser, error)
	Quit() error
	Close() error
}

DialAndSend ,首先調(diào)用Dial方法創(chuàng)建連接,然后發(fā)送郵件,最后關(guān)閉鏈接,如果要頻繁發(fā)郵件,那么是否保持長連接更好了?這里的Dial 調(diào)用了smtp.NewClient 創(chuàng)建smtp.Client對象c,然后調(diào)用c.Hello ,c.Auth,send 實(shí)際是調(diào)用c.Mail,c.Rcpt,c.Data,那么我們可以自己調(diào)用Dial方法 然后循環(huán)調(diào)用send方法,最后在close。

代碼如下:

 
func SendMailByGomailTwo() {
	d := gomail.NewDialer("smtp.163.com", 25, "dz45693@163.com", "xxxx")
	m := gomail.NewMessage()
	m.SetAddressHeader("From", "dz45693@163.com", "dz45693")
	m.SetHeader("To", "dz45693@sina.com")
	m.SetHeader("Subject", "hello SendMailByGomailtwo!")
	m.Embed("d:\\Downloads\\1.png")
	m.SetBody("text/html", "此處為正文121333!SendMailByGomailtwo")
	s, err := d.Dial()
	if err != nil {
		panic(err)
	}
	defer s.Close()
	err = gomail.Send(s, m)
	if err != nil {
		panic(err)
	}
	m.Reset()
	m.SetAddressHeader("From", "dz45693@163.com", "dz45693")
	m.SetHeader("To", "dz45693@sina.com")
	m.SetHeader("Subject", "hello SendMailByGomailthree!")
	m.Embed("d:\\Downloads\\2.png")
	m.SetBody("text/html", "此處為正文1SendMailByGomailthreeSendMailByGomailthree!")
	err = gomail.Send(s, m)
	if err != nil {
		panic(err)
	}
}

運(yùn)行結(jié)果:

以上就是go smtp實(shí)現(xiàn)郵件發(fā)送示例詳解的詳細(xì)內(nèi)容,更多關(guān)于go smtp郵件發(fā)送的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Go語言中排序的3種實(shí)現(xiàn)方法

    Go語言中排序的3種實(shí)現(xiàn)方法

    在寫代碼過程中,排序是經(jīng)常會遇到的需求,這篇文章主要為大家介紹三種常用的方法,文中的示例代碼簡潔易懂,需要的小伙伴可以參考下
    2023-08-08
  • Go語言中的反射原理解析與應(yīng)用

    Go語言中的反射原理解析與應(yīng)用

    反射(Reflection)是計(jì)算機(jī)科學(xué)中的一個(gè)重要概念,它允許程序在運(yùn)行時(shí)檢查變量和值,獲取它們的類型信息,并且能夠修改它們,本文將結(jié)合實(shí)際案例,詳細(xì)介紹Go語言中反射的基本概念、關(guān)鍵函數(shù)以及使用場景,需要的朋友可以參考下
    2024-10-10
  • Go語言中日志統(tǒng)一處理詳解

    Go語言中日志統(tǒng)一處理詳解

    在現(xiàn)代軟件開發(fā)中,日志記錄是一項(xiàng)至關(guān)重要的任務(wù),它不僅幫助開發(fā)人員診斷問題,還有助于監(jiān)控和維護(hù)應(yīng)用程序,本文主要來和大家聊聊日志的統(tǒng)一處理,感興趣的小伙伴可以了解下
    2024-01-01
  • Go語言配置數(shù)據(jù)庫連接池的實(shí)現(xiàn)

    Go語言配置數(shù)據(jù)庫連接池的實(shí)現(xiàn)

    本文內(nèi)容我們將解釋連接池背后是如何工作的,并探索如何配置數(shù)據(jù)庫能改變或優(yōu)化其性能。文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • go語言base64用法實(shí)例

    go語言base64用法實(shí)例

    這篇文章主要介紹了go語言base64用法,實(shí)例分析了Go語言base64編碼的實(shí)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • Go語言學(xué)習(xí)之WaitGroup用法詳解

    Go語言學(xué)習(xí)之WaitGroup用法詳解

    Go語言中的?WaitGroup?和?Java?中的?CyclicBarrier、CountDownLatch?非常類似。本文將詳細(xì)為大家講講WaitGroup的用法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-06-06
  • golang語言編碼規(guī)范的實(shí)現(xiàn)

    golang語言編碼規(guī)范的實(shí)現(xiàn)

    這篇文章主要介紹了golang語言編碼規(guī)范的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Go 自定義package包設(shè)置與導(dǎo)入操作

    Go 自定義package包設(shè)置與導(dǎo)入操作

    這篇文章主要介紹了Go 自定義package包設(shè)置與導(dǎo)入操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • golang int64轉(zhuǎn)int的方法

    golang int64轉(zhuǎn)int的方法

    這篇文章主要介紹了golang int64轉(zhuǎn)int,本文給大家提供兩種方法 ,將 golang int64 轉(zhuǎn)換為golang int,結(jié)合實(shí)例代碼給大家分享轉(zhuǎn)換方法,需要的朋友可以參考下
    2023-01-01
  • go Http Post 發(fā)送文件流案例

    go Http Post 發(fā)送文件流案例

    這篇文章主要介紹了go Http Post 發(fā)送文件流案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12

最新評論

宁强县| 沈丘县| 化隆| 黔南| 海南省| 元江| 临安市| 农安县| 贺兰县| 湖北省| 七台河市| 科技| 龙川县| 张北县| 五河县| 黔江区| 定西市| 固镇县| 五台县| 青田县| 达州市| 兴海县| 前郭尔| 铁岭县| 四会市| 普格县| 儋州市| 浑源县| 安国市| 英超| 大荔县| 南华县| 温宿县| 丽水市| 乌鲁木齐市| 越西县| 黎城县| 梅州市| 吉隆县| 蒙城县| 家居|