C语言检查本机公网IP并发送邮件(2)

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 *emailTo,char *emailFrom,char *emailPd,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\n",
  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..."<<endl;
  Sleep(2000);
  sockfd = OpenSocket((struct sockaddr *)&their_addr);
  memset(rbuf, 0, 1500);
 }

cout<<rbuf<<endl;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6a6d74f24bb90fb8350abc27d084583a.html