一步一步从原理跟我学邮件收取及发送 11.完整的发送示例与go语言

时间:2021-10-12 00:46:59

经过了这个系列的前几篇文章的学习,现在要写出一个完整的 smtp 邮件发送过程简直易如反掌。
    例如我们可以轻松地写出以下的纯 C 语言代码(引用的其他C语言文件请看文末的 github 地址):

#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <winsock.h> #include "lstring.c"
#include "socketplus.c"
#include "lstring_functions.c"
#include "base64_functions.c" //vc 下要有可能要加 lib
//#pragma comment (lib,"*.lib")
//#pragma comment (lib,"libwsock32.a")
//#pragma comment (lib,"libwsock32.a") //SOCKET gSo = 0;
SOCKET gSo = -; //收取一行,可再优化
lstring * RecvLine(SOCKET so, struct MemPool * pool, lstring ** _buf)
{
int i = ;
int index = -;
int canread = ;
lstring * r = NULL;
lstring * s = NULL;
lstring * buf = *_buf; for (i=;i<;i++) //安全起见,不用 while ,用 for 一定次数就可以了
{
//index = pos("\n", buf);
index = pos(NewString("\r\n", pool), buf); if (index>-) break; canread = SelectRead_Timeout(so, );//是否可读取,时间//超时返回,单位为秒
if ( == canread) break; s = RecvBuf(so, pool);
buf->Append(buf, s);
} if (index < ) return NewString("", pool); r = substring(buf, , index);
buf = substring(buf, index + , Length(buf));
*_buf = buf;
return r;
}// //解码一行命令,这里比较简单就是按空格进行分隔就行了
//这是用可怕的指针运算的版本
void DecodeCmd(lstring * line, char sp, char ** cmds, int cmds_count)
{
int i = ;
int index = ;
int count = ; cmds[index] = line->str; for (i=; i<line->len; i++)
{
if (sp == line->str[i])
{
index++;
line->str[i] = '\0'; //直接修改为字符串结束符号,如果是只读的字符串这样做其实是不对的,不过效率很高 cmds[index] = line->str + i; //指针向后移动 if (i >= line->len - ) break;//如果是最后一定字符了就要退出,如果不是指针还要再移动一位
cmds[index] = line->str + i + ; count++;
if (count >= cmds_count) break; //不要大于缓冲区
} }// }// //读取多行结果
lstring * RecvMCmd(SOCKET so, struct MemPool * pool, lstring ** _buf)
{
int i = ;
int index = ;
int count = ;
lstring * rs;
char c4 = '\0'; //判断第4个字符 lstring * mline = NewString("", pool); for (i=; i<; i++)
{
rs = RecvLine(so, pool, _buf); //只收取一行 mline->Append(mline, rs);
LString_AppendConst(mline, "\r\n"); //printf("\r\nRecvMCmd:%s\r\n", rs->str); if (rs->len<) break; //长度要足够
c4 = rs->str[-]; //第4个字符
//if ('\x20' == c4) break; //"\xhh" 任意字符 二位十六进制//其实现在的转义符已经扩展得相当复杂,不建议用这个表示空格
if (' ' == c4) break; //第4个字符是空格就表示读取完了//也可以判断 "250[空格]" }// return mline;
}// void main()
{
int r;
mempool mem, * m;
lstring * s;
lstring * rs;
lstring * buf;
lstring * domain;
lstring * from;
lstring * to; char * cmds[] = {NULL};
int cmds_count = ; //-------------------------------------------------- mem = makemem(); m = &mem; //内存池,重要 buf = NewString("", m); //--------------------------------------------------
//直接装载各个 dll 函数
LoadFunctions_Socket(); InitWinSocket(); //初始化 socket, windows 下一定要有 gSo = CreateTcpClient();
r = ConnectHost(gSo, "newbt.net", );
//r = ConnectHost(gSo, "smtp.163.com", 25); //可以换成 163 的邮箱 if (r == ) printf("连接成功!\r\n"); //--------------------------------------------------
rs = RecvLine(gSo, m, &buf); //只收取一行 printf("\r\nRecvLine:");
printf(rs->str); printf("\r\n"); DecodeCmd(rs, ' ', cmds, cmds_count);
printf("\r\ndomain:%s\r\n", cmds[]); domain = NewString(cmds[], m); s = NewString("EHLO", m);
LString_AppendConst(s," ");
s->Append(s, domain); //去掉这一行试试,163 邮箱就会返回错误了
LString_AppendConst(s,"\r\n"); SendBuf(gSo, s->str, s->len); ////rs = RecvLine(gSo, m, &buf); //只收取一行
rs = RecvMCmd(gSo, m, &buf); //只收取一行 printf("\r\nRecvLine:");
printf(rs->str); printf("\r\n"); //--------------------------------------------------
//用 base64 登录
s = NewString("AUTH LOGIN\r\n", m);
SendBuf(gSo, s->str, s->len); rs = RecvLine(gSo, m, &buf); //只收取一行
printf("\r\nRecvLine:%s\r\n", rs->str); s = NewString("test1@newbt.net", m); //要换成你的用户名,注意 163 邮箱的话不要带后面的 @域名 部分
s = base64_encode(s);
LString_AppendConst(s,"\r\n");
SendBuf(gSo, s->str, s->len); rs = RecvLine(gSo, m, &buf); //只收取一行
printf("\r\nRecvLine:%s\r\n", rs->str); s = NewString("", m); //要换成您的密码
s = base64_encode(s);
LString_AppendConst(s,"\r\n");
SendBuf(gSo, s->str, s->len); rs = RecvLine(gSo, m, &buf); //只收取一行
printf("\r\nRecvLine:%s\r\n", rs->str); //--------------------------------------------------
//邮件内容
from = NewString("test1@newbt.net", m);
to = NewString("clq@newbt.net", m); s = NewString("MAIL FROM: <", m); s->Append(s, from); s->AppendConst(s, ">\r\n"); //注意"<" 符号和前面的空格。空格在协议中有和没有都可能,最好还是有
SendBuf(gSo, s->str, s->len);
rs = RecvLine(gSo, m, &buf); //只收取一行
printf("\r\nRecvLine:%s\r\n", rs->str); s = NewString("RCPT TO: <", m); s->Append(s, to); s->AppendConst(s, ">\r\n");
SendBuf(gSo, s->str, s->len);
rs = RecvLine(gSo, m, &buf); //只收取一行
printf("\r\nRecvLine:%s\r\n", rs->str); s = NewString("DATA\r\n", m);
SendBuf(gSo, s->str, s->len);
rs = RecvLine(gSo, m, &buf); //只收取一行
printf("\r\nRecvLine:%s\r\n", rs->str); s = NewString("From: \"test1@newbt.net\" <test1@newbt.net>\r\nTo: \"clq@newbt.net\" <clq@newbt.net>\r\nSubject: test\r\nDate: Sun, 21 Jan 2018 11:48:15 GMT\r\n\r\nHello World.\r\n", m);//邮件内容,正式的应该用一个函数生成
SendBuf(gSo, s->str, s->len); s = NewString("\r\n.\r\n", m); //邮件结束符
SendBuf(gSo, s->str, s->len); rs = RecvLine(gSo, m, &buf); //只收取一行
printf("\r\nRecvLine:%s\r\n", rs->str); //-------------------------------------------------- Pool_Free(&mem); //释放内存池 printf("gMallocCount:%d \r\n", gMallocCount); //看看有没有内存泄漏//简单的检测而已 //-------------------------------------------------- getch(); //getch().不过在VC中好象要用getch(),必须在头文件中加上<conio.h> }

运行结果如图:

一步一步从原理跟我学邮件收取及发送 11.完整的发送示例与go语言

好了,我们用其他语言也来一个吧。但是这里有个问题:java 有很完善的电子邮件实现,实在是没必要再写一个。我换用一下 go 语言吧,之所以用 golang ,那是因为 golang 现在真的是开发服务器程序的极佳选择,而因为它又非常的新,所以有些功能并不完善,特别是支持库方面离 java 还有比较远的距离。刚好在电子邮件方面就是,而我刚好最近 golang 又用得比较多。

用过 golang 发送邮件的同学一定都知道 go 语言中默认的 smtp 模块是无法在正常的 smtp 25 端口上去发送邮件的(有兴趣的网友可以自行用 163 的邮箱试试)。原因是 golang 本身起源就是为 google 公司的需求服务的,所以很多功能都先优先做了 google 需要的部分,而对电子邮件有一点了解的网友们应该都知道 google 的 gmail 是不支持常规的 smtp 25 端口的,它需要安全连接的 ssl 接口。所以大家如果搜索 golang 的 smtp 发送示例的话,基本上都是要进行一点改造的。其实这样的改造代码都不完善,最后都会注明有问题。这个问题其实源于 golang 的 smtp 源码(我看的是 1.7 版本)中对 "AUTH" 命令的实现与常规不太一样,它的实现后面跟了两个参数,而经过我们前几篇的文章,大家都知道实现的只有一个参数,那就是 "AUTH LOGIN"。知道了这一点,要改造 golang 的源码还是比较容易的。不过 golang 和 java 一样有点过度设计的意思,所以要看懂它的代码也不是太容易(不过 golang 中的各种协议代码设计得很精巧,远远不是 java 可比的)。所以我们既然已经知道了怎样自己写一个,那还不如自己明明白白的写一个出来。以下就是一个我写的示例,对协议有一定了解的同学很容易进行改写,为了方便大家理解我就没有设计成类了,大家可以自己动手:

package main //clq

//用于不加密环境的 smtp 发送电子邮件过程,因为不是所有的 smtp 环境都有加密支持的,不加密的适用范围更广一点,而且 smtp 服务器之间没有密码加密的过程
//(因为对方 smtp 服务器不可能知道你的密码)所以原有的 golang 1.7.3 net/smtp 过程就不适合 smtp 之间发送邮件 import (
"fmt"
"bufio"
// "crypto/tls"
"encoding/base64"
// "errors"
// "io"
"net"
// "net/smtp" //clq add
// "net/textproto"
"strings"
"strconv"
) 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); //writer.WriteString("EHLO\r\n");
//writer.Flush(); //host, _, _ := net.SplitHostPort(addr)
//return NewClient(conn, host)
return conn, reader, writer;
}// //收取一行,可再优化
//func RecvLine(conn *net.Conn) (string) {
//func RecvLine(conn net.Conn, reader * bufio.Reader) (string) {
func _RecvLine() (string) { //defer conn.Close();
////reader := bufio.NewReader(conn);
//reader := bufio.NewReaderSize(conn,409600) //line, err := reader.ReadString('\n'); //如何设定超时?
line, err := gRead.ReadString('\n'); //如何设定超时? if err != nil { return ""; } line = strings.Split(line, "\r")[]; //还要再去掉 "\r",其实不去掉也可以 return line;
}// func SendLine(line string){
gWrite.WriteString(line + "\r\n");
gWrite.Flush();
}// //解码一行命令,这里比较简单就是按空格进行分隔就行了
func DecodeCmd(line string, sp string) ([]string){ //String[] tmp = line.split(sp); //用空格分开//“.”和“|”都是转义字符,必须得加"\\";//不一定是空格也有可能是其他的
//String[] cmds = {"", "", "", "", ""}; //先定义多几个,以面后面使用时产生异常 tmp := strings.Split(line, sp);
//var cmds = [5]string{"", "", "", "", ""}; //先定义多几个,以面后面使用时产生异常
var cmds = []string{"", "", "", "", ""}; //先定义多几个,以面后面使用时产生异常
//i:=0;
for i:=;i<len(tmp);i++ {
if i >= len(cmds) { break;}
cmds[i] = tmp[i];
}
return []string(cmds);
}// //读取多行结果
func RecvMCmd() (string) {
i := ;
//index := 0;
//count := 0;
rs := "";
//var c rune='\r';
//var c4 rune = '\0'; //判断第4个字符//golang 似乎不支持这种表示 mline := ""; for i=; i<; i++ {
rs = _RecvLine(); //只收取一行 mline = mline + rs + "\r\n"; //printf("\r\nRecvMCmd:%s\r\n", rs->str); if len(rs)< {break;} //长度要足够
c4 := rs[-]; //第4个字符
//if ('\x20' == c4) break; //"\xhh" 任意字符 二位十六进制//其实现在的转义符已经扩展得相当复杂,不建议用这个表示空格
if ' ' == c4 { break;} //第4个字符是空格就表示读取完了//也可以判断 "250[空格]" }// return mline; }// //简单的测试一下 smtp
func test_smtp() { //连接
//gConn, gRead, gWrite = Connect("newbt.net", 25);
//gConn, gRead, gWrite = Connect("newbt.net", 25);
gConn, gRead, gWrite = Connect("smtp.163.com", ); //收取一行
line := _RecvLine();
fmt.Println("recv:" + line); //解码一下,这样后面的 EHLO 才能有正确的第二个参数
cmds := DecodeCmd(line, " ");
domain := cmds[]; //要从对方的应答中取出域名//空格分开的各个命令参数中的第二个 //发送一个命令
//SendLine("EHLO"); //163 这样是不行的,一定要有 domain
SendLine("EHLO" + " " + domain); //domain 要求其实来自 HELO 命令//HELO <SP> <domain> <CRLF> //收取多行
//line = _RecvLine();
line = RecvMCmd();
fmt.Println("recv:" + line); //--------------------------------------------------
//用 base64 登录
SendLine("AUTH LOGIN"); //收取一行
line = _RecvLine();
fmt.Println("recv:" + line); //s :="test1@newbt.net"; //要换成你的用户名,注意 163 邮箱的话不要带后面的 @域名 部分
s :="clq_test"; //要换成你的用户名,注意 163 邮箱的话不要带后面的 @域名 部分
s = base64.StdEncoding.EncodeToString([]byte(s));
//s = base64_encode(s);
SendLine(s); //收取一行
line = _RecvLine();
fmt.Println("recv:" + line); s = ""; //要换成您的密码
//s = base64_encode(s);
s = base64.StdEncoding.EncodeToString([]byte(s));
SendLine(s); //收取一行
line = _RecvLine();
fmt.Println("recv:" + line); //--------------------------------------------------
//邮件内容
//from := "test1@newbt.net";
from := "clq_test@163.com";
to := "clq@newbt.net"; 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) // = "From: \"test1@newbt.net\" <test1@newbt.net>\r\nTo: \"clq@newbt.net\" <clq@newbt.net>\r\nSubject: test golang\r\nDate: Sun, 21 Jan 2018 11:48:15 GMT\r\n\r\nHello World.\r\n";//邮件内容,正式的应该用一个函数生成
s = MakeMail(from,to,"test golang","Hello World.");
SendLine(s); s = "\r\n.\r\n"; //邮件结束符
SendLine(s); //收取一行
line = _RecvLine();
fmt.Println("recv:" + line) }// //这只是个简单的内容,真实的邮件内容复杂得多
func MakeMail(from,to,subject,text string)(string) {
//s := "From: \"test1@newbt.net\" <test1@newbt.net>\r\nTo: \"clq@newbt.net\" <clq@newbt.net>\r\nSubject: test golang\r\nDate: Sun, 21 Jan 2018 11:48:15 GMT\r\n\r\nHello World.\r\n";//邮件内容,正式的应该用一个函数生成
s := "From: \"" + from + "\"\r\nTo: \"" + to + "\" " + to + "\r\nSubject: " + subject +
"\r\nDate: Sun, 21 Jan 2018 11:48:15 GMT\r\n\r\n" + //内容前是两个回车换行
text + "\r\n"; return s; }//

这份代码可以直接使用在 163 的邮箱上,以下是 newbt 邮箱收到的 163 发送的邮件的真实截图:

一步一步从原理跟我学邮件收取及发送 11.完整的发送示例与go语言

不过用到真实环境中,大家要再测试一下超时的情况,可以考虑自己加点超时,然后生成邮件的时间那里注意一下就差不多了。我后面还会给出一个直接修改自 go 源码的示例。

既然说到了 gmail 是要 ssl 支持的,那么怎样开发一个支持 ssl 的发送过程呢。其实一点也不难,ssl 不过是 socket 过程的加密版本而已,有兴趣的同学大家可以看我的如下几篇文章:
OpenSSL解惑1:原理 https://baijiahao.baidu.com/s?id=1591824116725476286&wfr=spider&for=pc

OpenSSL解惑2:如何强制选择协议的版本 https://baijiahao.baidu.com/s?id=1591912273348927453&wfr=spider&for=pc

OpenSSL解惑3:SSL_read的阻塞超时与它是否等同于recv函 https://baijiahao.baidu.com/s?id=1592012048270657934&wfr=spider&for=pc

(我个人觉得第一二篇比较重要,百家号目前的问题比较多,链接代码什么的都不太好处理,大家将就看吧)

总的来说,增加了 ssl 连接过程后再将 recv 函数变成 SSL_read,send 函数变成 SS_send 就可以了。对 gmail 的支持很多年前我就在 eEmail 中实现过了,不过现在 ssl 升级得比较多,估计也不能用了吧,以后等 gmail 能在国内访问了再修改了。关于协议升级的问题大家也可以看上面的提到的几篇 openssl 文章。如果使用 openssl 开发的话,要支持新的协议是非常简单的,用 golang 的话那就更不用说了。ssl 的示例,因为实在来不及了,所以以后再给出吧。

另外,虽然我们没有给出 java 的示例,不过有了前面的几篇文章为基础相信大家可以轻松地写出来。

再另外,我看过网友们对 golang smtp 的改造,说句不客气的,大部分都不太正确,具体原因前面已经说了过。我的做法是直接修改 golang 的源码,幸好 goalng 和以前的 delphi 一样可以方便地在不改动源码的情况下使用修改出来的另一份拷贝。我的方法是复制出来然后改掉 package 就可以了,以下是我修改过的 go1.7.3 的 smtp 源码,可以直接使用的,修改的地方其实很少,已经在 newbt 邮箱和 163 邮箱上使用过。调用的方法如下(其中 loginAuth  来自网络,不过实际上是有误的):

type loginAuth struct {
username, password string
} func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
} func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
//return "LOGIN", []byte{}, nil
//return "LOGIN", []byte(a.username), nil //clq 原作者说这个就 ok, 其实那只是对 163 邮箱, 大多数邮件服务器还要改 smtp.go 文件本身
//return "LOGIN\r\n", []byte(a.username), nil
return "LOGIN", []byte{}, nil
} //clq 这个步骤会一直被调用直到成功或者错误
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { fmt.Println("smtp server:", string(fromServer)); s_fromServer := strings.ToLower(string(fromServer)); if more {
//switch string(fromServer) {
switch s_fromServer {
//case "Username:":
case "username:":
return []byte(a.username), nil
//case "Password:":
case "password:":
return []byte(a.password), nil
}//switch
}//if
return nil, nil
}// func SendMail_t1() {
auth := LoginAuth("test1@newbt.net", "");
to := []string{"clq@newbt.net"} mimes := //这里写上邮件内容
err2 := smtp_SendMail_new("newbt.net:25", auth, "clq@newbt.net", to, []byte(mimes)); fmt.Println(err);
}//

新的文件名为 smtp_new.go ,内容如下(我其实还是推荐大家自己改前面的代码,那样更好把握,所以这份代码我默认折叠了):

 // Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // Package smtp implements the Simple Mail Transfer Protocol as defined in RFC 5321.
// It also implements the following extensions:
// 8BITMIME RFC 1652
// AUTH RFC 2554
// STARTTLS RFC 3207
// Additional extensions may be handled by clients.
//
// The smtp package is frozen and not accepting new features.
// Some external packages provide more functionality. See:
//
// https://godoc.org/?q=smtp
//package smtp
package main //clq //用于不加密环境的 smtp 发送电子邮件过程,因为不是所有的 smtp 环境都有加密支持的,不加密的适用范围更广一点,而且 smtp 服务器之间没有密码加密的过程
//(因为对方 smtp 服务器不可能知道你的密码)所以原有的 golang 1.7.3 net/smtp 过程就不适合 smtp 之间发送邮件
//修改自 smtp.go[1.7.3] , 原始文件为 go1.7.3 的 net/smtp
//原文件在 code, msg64, err := c.cmd(0, "AUTH %s %s", mech, resp64) 处有误,因为第一个 AUTH 后面只有一个字符(指不加密的情况下) import (
"crypto/tls"
"encoding/base64"
"errors"
"io"
"net"
"net/smtp" //clq add
"net/textproto"
"strings"
) // A Client represents a client connection to an SMTP server.
type Client struct {
// Text is the textproto.Conn used by the Client. It is exported to allow for
// clients to add extensions.
Text *textproto.Conn
// keep a reference to the connection so it can be used to create a TLS
// connection later
conn net.Conn
// whether the Client is using TLS
tls bool
serverName string
// map of supported extensions
ext map[string]string
// supported auth mechanisms
auth []string
localName string // the name to use in HELO/EHLO
didHello bool // whether we've said HELO/EHLO
helloError error // the error from the hello
} // Dial returns a new Client connected to an SMTP server at addr.
// The addr must include a port, as in "mail.example.com:smtp".
func Dial(addr string) (*Client, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
host, _, _ := net.SplitHostPort(addr)
return NewClient(conn, host)
} // NewClient returns a new Client using an existing connection and host as a
// server name to be used when authenticating.
func NewClient(conn net.Conn, host string) (*Client, error) {
text := textproto.NewConn(conn)
_, _, err := text.ReadResponse()
if err != nil {
text.Close()
return nil, err
}
c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"}
return c, nil
} // Close closes the connection.
func (c *Client) Close() error {
return c.Text.Close()
} // hello runs a hello exchange if needed.
func (c *Client) hello() error {
if !c.didHello {
c.didHello = true
err := c.ehlo()
if err != nil {
c.helloError = c.helo()
}
}
return c.helloError
} // Hello sends a HELO or EHLO to the server as the given host name.
// Calling this method is only necessary if the client needs control
// over the host name used. The client will introduce itself as "localhost"
// automatically otherwise. If Hello is called, it must be called before
// any of the other methods.
func (c *Client) Hello(localName string) error {
if c.didHello {
return errors.New("smtp: Hello called after other methods")
}
c.localName = localName
return c.hello()
} // cmd is a convenience function that sends a command and returns the response
func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, string, error) {
id, err := c.Text.Cmd(format, args...)
if err != nil {
return , "", err
}
c.Text.StartResponse(id)
defer c.Text.EndResponse(id)
code, msg, err := c.Text.ReadResponse(expectCode)
return code, msg, err
} // helo sends the HELO greeting to the server. It should be used only when the
// server does not support ehlo.
func (c *Client) helo() error {
c.ext = nil
_, _, err := c.cmd(, "HELO %s", c.localName)
return err
} // ehlo sends the EHLO (extended hello) greeting to the server. It
// should be the preferred greeting for servers that support it.
func (c *Client) ehlo() error {
_, msg, err := c.cmd(, "EHLO %s", c.localName)
if err != nil {
return err
}
ext := make(map[string]string)
extList := strings.Split(msg, "\n")
if len(extList) > {
extList = extList[:]
for _, line := range extList {
args := strings.SplitN(line, " ", )
if len(args) > {
ext[args[]] = args[]
} else {
ext[args[]] = ""
}
}
}
if mechs, ok := ext["AUTH"]; ok {
c.auth = strings.Split(mechs, " ")
}
c.ext = ext
return err
} // StartTLS sends the STARTTLS command and encrypts all further communication.
// Only servers that advertise the STARTTLS extension support this function.
func (c *Client) StartTLS(config *tls.Config) error {
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(, "STARTTLS")
if err != nil {
return err
}
c.conn = tls.Client(c.conn, config)
c.Text = textproto.NewConn(c.conn)
c.tls = true
return c.ehlo()
} // TLSConnectionState returns the client's TLS connection state.
// The return values are their zero values if StartTLS did
// not succeed.
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool) {
tc, ok := c.conn.(*tls.Conn)
if !ok {
return
}
return tc.ConnectionState(), true
} // Verify checks the validity of an email address on the server.
// If Verify returns nil, the address is valid. A non-nil return
// does not necessarily indicate an invalid address. Many servers
// will not verify addresses for security reasons.
func (c *Client) Verify(addr string) error {
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(, "VRFY %s", addr)
return err
} // Auth authenticates a client using the provided authentication mechanism.
// A failed authentication closes the connection.
// Only servers that advertise the AUTH extension support this function.
func (c *Client) Auth(a smtp.Auth) error {
if err := c.hello(); err != nil {
return err
}
encoding := base64.StdEncoding
mech, resp, err := a.Start(&smtp.ServerInfo{c.serverName, c.tls, c.auth})
if err != nil {
c.Quit()
return err
}
resp64 := make([]byte, encoding.EncodedLen(len(resp)))
encoding.Encode(resp64, resp)
//code, msg64, err := c.cmd(0, "AUTH %s %s", mech, resp64); //clq 这里有误,标准就应该是先 'AUTH LOGIN' 后面并没有其他内容
code, msg64, err := c.cmd(, "AUTH %s", mech); //clq 这里有误,标准就应该是先 'AUTH LOGIN' 后面并没有其他内容//对于不加密的来说
for err == nil {
var msg []byte
switch code {
case :
msg, err = encoding.DecodeString(msg64)
case :
// the last message isn't base64 because it isn't a challenge
msg = []byte(msg64); //clq 对于不加密的来说这里一般就是返回登录成功了
default:
err = &textproto.Error{Code: code, Msg: msg64}
}
if err == nil {
resp, err = a.Next(msg, code == )
}
if err != nil {
// abort the AUTH
c.cmd(, "*")
c.Quit()
break
}
if resp == nil {
break
}
resp64 = make([]byte, encoding.EncodedLen(len(resp)))
encoding.Encode(resp64, resp)
code, msg64, err = c.cmd(, string(resp64))
}
return err
} // Mail issues a MAIL command to the server using the provided email address.
// If the server supports the 8BITMIME extension, Mail adds the BODY=8BITMIME
// parameter.
// This initiates a mail transaction and is followed by one or more Rcpt calls.
func (c *Client) Mail(from string) error {
if err := c.hello(); err != nil {
return err
}
cmdStr := "MAIL FROM:<%s>"
if c.ext != nil {
if _, ok := c.ext["8BITMIME"]; ok {
cmdStr += " BODY=8BITMIME"
}
}
_, _, err := c.cmd(, cmdStr, from)
return err
} // Rcpt issues a RCPT command to the server using the provided email address.
// A call to Rcpt must be preceded by a call to Mail and may be followed by
// a Data call or another Rcpt call.
func (c *Client) Rcpt(to string) error {
_, _, err := c.cmd(, "RCPT TO:<%s>", to)
return err
} type dataCloser struct {
c *Client
io.WriteCloser
} func (d *dataCloser) Close() error {
d.WriteCloser.Close()
_, _, err := d.c.Text.ReadResponse()
return err
} // Data issues a DATA command to the server and returns a writer that
// can be used to write the mail headers and body. The caller should
// close the writer before calling any more methods on c. A call to
// Data must be preceded by one or more calls to Rcpt.
func (c *Client) Data() (io.WriteCloser, error) {
_, _, err := c.cmd(, "DATA")
if err != nil {
return nil, err
}
return &dataCloser{c, c.Text.DotWriter()}, nil
} var testHookStartTLS func(*tls.Config) // nil, except for tests // SendMail connects to the server at addr, switches to TLS if
// possible, authenticates with the optional mechanism a if possible,
// and then sends an email from address from, to addresses to, with
// message msg.
// The addr must include a port, as in "mail.example.com:smtp".
//
// The addresses in the to parameter are the SMTP RCPT addresses.
//
// The msg parameter should be an RFC 822-style email with headers
// first, a blank line, and then the message body. The lines of msg
// should be CRLF terminated. The msg headers should usually include
// fields such as "From", "To", "Subject", and "Cc". Sending "Bcc"
// messages is accomplished by including an email address in the to
// parameter but not including it in the msg headers.
//
// The SendMail function and the the net/smtp package are low-level
// mechanisms and provide no support for DKIM signing, MIME
// attachments (see the mime/multipart package), or other mail
// functionality. Higher-level packages exist outside of the standard
// library.
//func SendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
func smtp_SendMail_new(addr string, a smtp.Auth, from string, to []string, msg []byte) error { //clq 这段注释是说, msg 包含了 mime 的所有内容
c, err := Dial(addr)
if err != nil {
return err
}
defer c.Close()
if err = c.hello(); err != nil {
return err
}
if ok, _ := c.Extension("STARTTLS"); ok {
config := &tls.Config{ServerName: c.serverName}
if testHookStartTLS != nil {
testHookStartTLS(config)
}
if err = c.StartTLS(config); err != nil {
return err
}
}
if a != nil && c.ext != nil {
if _, ok := c.ext["AUTH"]; ok {
if err = c.Auth(a); err != nil { //clq c.Auth(a) 这个也有交互过程
return err
}
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
} // Extension reports whether an extension is support by the server.
// The extension name is case-insensitive. If the extension is supported,
// Extension also returns a string that contains any parameters the
// server specifies for the extension.
func (c *Client) Extension(ext string) (bool, string) {
if err := c.hello(); err != nil {
return false, ""
}
if c.ext == nil {
return false, ""
}
ext = strings.ToUpper(ext)
param, ok := c.ext[ext]
return ok, param
} // Reset sends the RSET command to the server, aborting the current mail
// transaction.
func (c *Client) Reset() error {
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(, "RSET")
return err
} // Quit sends the QUIT command and closes the connection to the server.
func (c *Client) Quit() error {
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(, "QUIT")
if err != nil {
return err
}
return c.Text.Close()
}

可以看到 golang 的代码是写得真简洁,所以也比较好改。

一定有网友觉得我发送部分说得太多太久了,好吧,那我们下一篇插播一下 pop3 接收,到时候大家就会明白,其实还有好多内容没讲。

完整代码大家可以到以下 github 地址下载或查看:    
https://github.com/clqsrc/c_lib_lstring/tree/master/email_book/book_11

--------------------------------------------------

版权声明:

本系列文章已授权百家号 "clq的程序员学前班" .