1. 创建socket套接字
<pre name="code" class="cpp"> SOCKET s = socket(PF_INET,SOCK_STREAM,0);
2.获取smtp服务器地址
<pre name="code" class="cpp"> sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(25);
hostent* hptr = gethostbyname("smtp.163.com");
memcpy(&sin.sin_addr.S_un.S_addr, hptr->h_addr_list[0], hptr->h_length);
3. connect服务器
<pre name="code" class="cpp"> connect(s, (sockaddr*)&sin, sizeof(sin))
4. 发送helo命令,相当于向服务器打招呼
char bufferHello[] = "helo ****\r\n";
cout << "helo ehlo..." << endl;
send(s, bufferHello, strlen(bufferHello), 0);
5. 发送验证请求
<pre name="code" class="cpp"> char bufferLogin[] = "auth login\r\n";
cout << "auth login..." << endl;
send(s, bufferLogin, strlen(bufferLogin),0);
6. 发送账号,密码
<pre name="code" class="cpp"> char bufferUserName[] = "********\r\n"; // 注意,星号应该是邮箱账号、密码的base64编码
char bufferpsw[] = "**********\r\n";
send(s,bufferUserName,strlen(bufferUserName),0);
send(s,bufferpsw,strlen(bufferpsw),0);
7. 指示邮件的来源和去处
<pre name="code" class="cpp"> char bufferMailFrom[] = "mail from:<***********>\r\n"; //邮箱账号
send(s, bufferMailFrom, strlen(bufferMailFrom), 0);
char bufferRcptTo[] = "rcpt to:<***********>\r\n";
send(s, bufferRcptTo, strlen(bufferRcptTo), 0);
8. 发送data命令表示正文开始
<pre name="code" class="cpp"> char bufferData[] = "data\r\n";
send(s, bufferData,strlen(bufferData), 0);
9. 发送正文和结束标志
char bufferSubject[] = "subject:hello i am jammgit\r\n";
send(s, bufferSubject, strlen(bufferSubject), 0);
char bufferPoint[] = ".\r\n";
send(s, bufferPoint, strlen(bufferPoint), 0);