C言語で簡単なSMTPメール送信を実現
//C SMTP
#include
#include
#include
#include
using namespace std;
/*
#pragma , 。
。
C , 。
#pragma , 。
: #pragma Para。 Para 。
*/
#pragma comment(lib, "ws2_32.lib")
// :http://blog.csdn.net/CL18652469346/article/details/53199167
struct Base64Date6
{
unsigned int d4:6;
unsigned int d3:6;
unsigned int d2:6;
unsigned int d1:6;
};
// base64
char ConvertToBase64 (char c6);
void EncodeBase64 (char *dbuf, char *buf128, int len);
void SendMail (char *email,char *body);
int OpenSocket (struct sockaddr *addr);
int main()
{
char EmailTo[] = "[email protected]";
char EmailContents[] = "From: \"lucy\"\r
"
"To: \"dasiy\"\r
"
"Subject: Hello\r
\r
"
"Hello World, Hello Email!";
SendMail(EmailTo, EmailContents);
return 0;
}
char ConvertToBase64(char uc)
{
if(uc < 26)
{
return 'A' + uc;
}
if(uc < 52)
{
return 'a' + (uc - 26);
}
if(uc < 62)
{
return '0' + (uc - 52);
}
if(uc == 62)
{
return '+';
}
return '/';
}
// base64
void EncodeBase64(char *dbuf, char *buf128, int len)
{
struct Base64Date6 *ddd = NULL;
int i = 0;
char buf[256] = {0};
char *tmp = NULL;
char cc = '\0';
memset(buf, 0, 256);
strcpy_s(buf, 256, buf128);
for(i = 1; i <= len/3; i++)
{
tmp = buf + (i-1)*3;
cc = tmp[2];
tmp[2] = tmp[0];
tmp[0] = cc;
ddd = (struct Base64Date6 *)tmp;
dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1);
dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)ddd->d2);
dbuf[(i-1)*4+2] = ConvertToBase64((unsigned int)ddd->d3);
dbuf[(i-1)*4+3] = ConvertToBase64((unsigned int)ddd->d4);
}
if(len % 3 == 1)
{
tmp = buf + (i-1)*3;
cc = tmp[2];
tmp[2] = tmp[0];
tmp[0] = cc;
ddd = (struct Base64Date6 *)tmp;
dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1);
dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)ddd->d2);
dbuf[(i-1)*4+2] = '=';
dbuf[(i-1)*4+3] = '=';
}
if(len%3 == 2)
{
tmp = buf+(i-1)*3;
cc = tmp[2];
tmp[2] = tmp[0];
tmp[0] = cc;
ddd = (struct Base64Date6 *)tmp;
dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1);
dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)ddd->d2);
dbuf[(i-1)*4+2] = ConvertToBase64((unsigned int)ddd->d3);
dbuf[(i-1)*4+3] = '=';
}
return;
}
//
void SendMail(char *email, char *body)
{
int sockfd = {0};
char buf[1500] = {0};
char rbuf[1500] = {0};
char login[128] = {0};
char pass[128] = {0};
WSADATA WSAData;
struct sockaddr_in their_addr = {0};
WSAStartup(MAKEWORD(2, 2), &WSAData);
memset(&their_addr, 0, sizeof(their_addr));
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(25);
hostent* hptr = gethostbyname("smtp.163.com");
memcpy(&their_addr.sin_addr.S_un.S_addr, hptr->h_addr_list[0], hptr->h_length);
printf("IP of smpt.163.com is : %d:%d:%d:%d
",
their_addr.sin_addr.S_un.S_un_b.s_b1,
their_addr.sin_addr.S_un.S_un_b.s_b2,
their_addr.sin_addr.S_un.S_un_b.s_b3,
their_addr.sin_addr.S_un.S_un_b.s_b4);
// , , 2
sockfd = OpenSocket((struct sockaddr *)&their_addr);
memset(rbuf, 0, 1500);
while(recv(sockfd, rbuf, 1500, 0) == 0)
{
cout<<"reconnect..."<2);
sockfd = OpenSocket((struct sockaddr *)&their_addr);
memset(rbuf, 0, 1500);
}
cout<// EHLO
memset(buf, 0, 1500);
sprintf_s(buf, 1500, "EHLO HYL-PC\r
");
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"EHLO REceive: "<// AUTH LOGIN
memset(buf, 0, 1500);
sprintf_s(buf, 1500, "AUTH LOGIN\r
");
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Auth Login Receive: "<// USER
memset(buf, 0, 1500);
sprintf_s(buf, 1500, "[email protected]");//
memset(login, 0, 128);
EncodeBase64(login, buf, strlen(buf));
sprintf_s(buf, 1500, "%s\r
", login);
send(sockfd, buf, strlen(buf), 0);
cout<<"Base64 UserName: "<memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"User Login Receive: "<// PASSWORD
sprintf_s(buf, 1500, "xxxxxxxxxxxxxx");//
memset(pass, 0, 128);
EncodeBase64(pass, buf, strlen(buf));
sprintf_s(buf, 1500, "%s\r
", pass);
send(sockfd, buf, strlen(buf), 0);
cout<<"Base64 Password: "<memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Send Password Receive: "<// MAIL FROM
memset(buf, 0, 1500);
sprintf_s(buf, 1500, "MAIL FROM: \r
" );
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"set Mail From Receive: "<// RCPT TO
sprintf_s(buf, 1500, "RCPT TO:\r
", email);
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Tell Sendto Receive: "<// DATA
sprintf_s(buf, 1500, "DATA\r
");
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Send Mail Prepare Receive: "<// ,\r
.\r
sprintf_s(buf, 1500, "%s\r
.\r
", body);
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Send Mail Receive: "<// QUIT
sprintf_s(buf, 1500, "QUIT\r
");
send(sockfd, buf, strlen(buf), 0);
memset(rbuf, 0, 1500);
recv(sockfd, rbuf, 1500, 0);
cout<<"Quit Receive: "<//
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!"<exit(-1);
}
if(connect(sockfd, addr, sizeof(struct sockaddr)) < 0)
{
cout<<"Connect sockfd(TCP) error!"<exit(-1);
}
return sockfd;
}