// EHLO
memset(buf, 0, 1500);
sprintf_s(buf, 1500, "EHLO HYL-PC\r\n");
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"EHLO REceive: "<<rbuf<<endl;
// AUTH LOGIN
memset(buf, 0, 1500);
sprintf_s(buf, 1500, "AUTH LOGIN\r\n");
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Auth Login Receive: "<<rbuf<<endl;
// USER
memset(buf, 0, 1500);
sprintf_s(buf, 1500, emailFrom);//你的邮箱账号
memset(login, 0, 128);
EncodeBase64(login, buf, strlen(buf));
sprintf_s(buf, 1500, "%s\r\n", login);
send(sockfd, buf, strlen(buf), 0);
cout<<"Base64 UserName: "<<buf<<endl;
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"User Login Receive: "<<rbuf<<endl;
// PASSWORD
sprintf_s(buf, 1500, emailPd);//你的邮箱密码
memset(pass, 0, 128);
EncodeBase64(pass, buf, strlen(buf));
sprintf_s(buf, 1500, "%s\r\n", pass);
send(sockfd, buf, strlen(buf), 0);
cout<<"Base64 Password: "<<buf<<endl;
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Send Password Receive: "<<rbuf<<endl;
// MAIL FROM
char mailFrom[100]="MAIL FROM: <";
strcat(mailFrom,emailFrom);
strcat(mailFrom,">\r\n");
memset(buf, 0, 1500);
sprintf_s(buf, 1500, mailFrom);
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"set Mail From Receive: "<<rbuf<<endl;
// RCPT TO 第一个收件人
sprintf_s(buf, 1500, "RCPT TO:<%s>\r\n", emailTo);
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Tell Sendto Receive: "<<rbuf<<endl;
// DATA 准备开始发送邮件内容
sprintf_s(buf, 1500, "DATA\r\n");
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Send Mail Prepare Receive: "<<rbuf<<endl;
// 发送邮件内容,\r\n.\r\n内容结束标记
sprintf_s(buf, 1500, "%s\r\n.\r\n", body);
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Send Mail Receive: "<<rbuf<<endl;
// QUIT
sprintf_s(buf, 1500, "QUIT\r\n");
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Quit Receive: "<<rbuf<<endl;
//清理工作
closesocket(sockfd);
WSACleanup();
return;
}
// 打开TCP Socket连接
int OpenSocket(struct sockaddr *addr)
{
int sockfd = 0;
sockfd=socket(PF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
cout<<"Open sockfd(TCP) error!"<<endl;
exit(-1);
}
if(connect(sockfd, addr, sizeof(struct sockaddr)) < 0)
{
cout<<"Connect sockfd(TCP) error!"<<endl;
exit(-1);
}
return sockfd;
}
将C语言梳理一下,分布在以下10个章节中:
Linux-C成长之路(一):Linux下C编程概要
Linux-C成长之路(十):其他高级议题