脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - go smtp实现邮件发送示例详解

go smtp实现邮件发送示例详解

2022-10-09 13:34dz45693 Golang

这篇文章主要为大家介绍了go smtp实现邮件发送示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

smtp指令

书接上文邮件实现详解,这里我们及我们简单复习一下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 smtp实现邮件发送示例详解

 

go demo

好,那我们下现在用go实现代码让如下:这里只是一个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
} //
//收取一行,可再优化
func RecvLine() string {
	line, err := gRead.ReadString("
") //如何设定超时?
	if err != nil {
		fmt.Print(err)
		return ""
	}
	line = strings.Split(line, "
")[0] //还要再去掉 "
",其实不去掉也可以
	return line
}
func SendLine(line string) {
	gWrite.WriteString(line + "
")
	gWrite.Flush()
}
//解码一行命令,这里比较简单就是按空格进行分隔就行了
func DecodeCmd(line string, sp string) []string {
	tmp := strings.Split(line, sp)
	var cmds = []string{"", "", "", "", ""} //先定义多几个,以面后面使用时产生异常
	for i := 0; i < len(tmp); i++ {
		if i >= len(cmds) {
			break
		}
		cmds[i] = tmp[i]
	}
	return cmds
}
//读取多行结果
func RecvMCmd() string {
	i := 0
	rs := ""
	mLine := ""
	for i = 0; i < 50; i++ {
		rs = RecvLine() //只收取一行
		mLine = mLine + rs + "
"
		if len(rs) < 4 {
			break
		} //长度要足够
		c4 := rs[4-1] //第4个字符
		if " " == c4 {
			break
		} //第4个字符是空格就表示读取完了//也可以判断 "250[空格]"
	}
	return mLine
}
//简单的测试一下 smtp
func testSmtp() {
	//连接
	gConn, gRead, gWrite = Connect("smtp.163.com", 25)
	defer gConn.Close()
	//收取一行
	line := RecvLine()
	fmt.Println("recv:" + line)
	//解码一下,这样后面的 EHLO 才能有正确的第二个参数
	cmds := DecodeCmd(line, " ")
	domain := cmds[1] //要从对方的应答中取出域名//空格分开的各个命令参数中的第二个
	//发送一个命令
	SendLine("EHLO" + " " + domain) //domain 要求其实来自 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)
	//--------------------------------------------------
	//邮件内容
	from := "dz45693@163.com"
	to := "dz45693@sina.com"
	SendLine("MAIL FROM: <" + from + ">") //注意"<" 符号和前面的空格。空格在协议中有和没有都可能,最好还是有
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	SendLine("RCPT TO: <" + to + ">")
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	SendLine("DATA")
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
//发送邮件头
	SendLine("from:<dz45693@163.com>")
	SendLine("to:<dz45693@sina.com>")
	SendLine("subject:hello world")
	SendLine("") //发送空行 后面就是邮件体
	SendLine("This is the first email sent by hand using the SMTP protocol")
	SendLine(".") //邮件结束符
	//收取一行
	line = RecvLine()
	fmt.Println("recv:" + line)
	SendLine("quit") //链接推出
	line = RecvLine()
	fmt.Println("recv:" + line)
} //

运行结果如下:

go smtp实现邮件发送示例详解

 

sdk中SendMail方法

在go的sdk中提供了SendMail方法【发送邮件后这个方法会关闭链接】,实现如下:

go smtp实现邮件发送示例详解

 实现如下:

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

运行效果:

go smtp实现邮件发送示例详解

 使用第三方库gomail实现邮件的发送更多了解,

请前往: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:Downloads1.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)
	}
}

运行结果:

go smtp实现邮件发送示例详解

 

DialAndSend实现

来我们看看DialAndSend的实现如下: 

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 ,首先调用Dial方法创建连接,然后发送邮件,最后关闭链接,如果要频繁发邮件,那么是否保持长连接更好了?这里的Dial 调用了smtp.NewClient 创建smtp.Client对象c,然后调用c.Hello ,c.Auth,send 实际是调用c.Mail,c.Rcpt,c.Data,那么我们可以自己调用Dial方法 然后循环调用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:Downloads1.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:Downloads2.png")
	m.SetBody("text/html", "此处为正文1SendMailByGomailthreeSendMailByGomailthree!")
	err = gomail.Send(s, m)
	if err != nil {
		panic(err)
	}
}

运行结果:

go smtp实现邮件发送示例详解

以上就是go smtp实现邮件发送示例详解的详细内容,更多关于go smtp邮件发送的资料请关注服务器之家其它相关文章!

原文地址:https://blog.csdn.net/ma_jiang/article/details/123462735

延伸 · 阅读

精彩推荐
  • GolangGO语言如何手动处理TCP粘包详解

    GO语言如何手动处理TCP粘包详解

    最近在用golang开发人工客服系统的时候碰到了粘包问题,那么什么是粘包呢?下面这篇文章就来给大家介绍了关于GO语言如何手动处理TCP粘包的相关资料,...

    怪咖_OOP4342020-05-12
  • GolangGolang原生rpc(rpc服务端源码解读)

    Golang原生rpc(rpc服务端源码解读)

    本文主要介绍了Golang原生rpc(rpc服务端源码解读),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    鹿灏楷silves7082022-09-13
  • Golang使用GO操作MongoDB的方法

    使用GO操作MongoDB的方法

    这篇文章主要介绍了使用GO操作MongoDB,包括安装MongoDB驱动程序连接mongodb的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值...

    hubb7822022-10-07
  • Golanggolang coroutine 的等待与死锁用法

    golang coroutine 的等待与死锁用法

    这篇文章主要介绍了golang coroutine 的等待与死锁用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    yxw20147942021-06-14
  • GolangGo Error 嵌套实现创建方式

    Go Error 嵌套实现创建方式

    这篇文章主要介绍了Go Error 嵌套到底是怎么实现的?大家都知道创建error有两种方式分别是errors.new()另一种是fmt.errorf(),本文通过详细例子给大家介绍...

    AlwaysBeta11242022-08-30
  • GolangGin 框架快速创建静态文件下载Web服务

    Gin 框架快速创建静态文件下载Web服务

    本文主要介绍了Gin 框架快速创建静态文件下载Web服务,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    尹东勋11492022-01-22
  • GolangGolang 实现 Redis系列(六)如何实现 pipeline 模式的 redis 客户端

    Golang 实现 Redis系列(六)如何实现 pipeline 模式的 redis 客户端

    pipeline 模式的 redis 客户端需要有两个后台协程负责 tcp 通信,调用方通过 channel 向后台协程发送指令,并阻塞等待直到收到响应,本文是使用 golang 实现 ...

    Finley8942021-08-14
  • Golanggo 下载非标准库包(部份包被墙了)到本地使用的方法

    go 下载非标准库包(部份包被墙了)到本地使用的方法

    今天小编就为大家分享一篇go 下载非标准库包(部份包被墙了)到本地使用的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    Lnnnnnnnnnnnnnnnnnn4432020-05-26