Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7722933
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2014-12-31 15:17:11


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <netdb.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <sys/fcntl.h>
  10. #include <sys/stat.h>
  11.  
  12. #define SOCKET_ERROR -1
  13. #define IPSTR "220.181.12.13"
  14. #define PRINT(x) printf("%s\n",(x))
  15.  
  16. int sockfd;
  17. int ret;
  18. char *send_data;
  19. char *recv_data;
  20. char From[128];
  21. char To[128];
  22. char Date[128];
  23. char Subject[512];
  24. char Context[6000]={0};
  25. char buffer[1024];

  26. int connectHost(const char *smtpaddr)
  27. {
  28.     struct hostent *host;
  29.     struct sockaddr_in servaddr;
  30.     sockfd = socket(AF_INET,SOCK_STREAM,0);
  31.     if(sockfd < 0)
  32.     {
  33.         printf("Create socket error!\n");
  34.         return -1;
  35.     }
  36.     
  37.     host = gethostbyname(smtpaddr); /*通过域名来获取主机的IP地址等信息*/
  38.     if(host == NULL)    /* gethostbyname失败 */
  39.     {
  40.         printf("server:%s \n",smtpaddr);
  41.         return -1;
  42.     }
  43.     
  44.     bzero(&servaddr,sizeof(servaddr));
  45.     servaddr.sin_family = AF_INET;
  46.     servaddr.sin_port = htons(25);
  47.     servaddr.sin_addr = *((struct in_addr *)host->h_addr); //使用域名
  48. #if 0    // 使用IP
  49.     if(inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 )
  50.     {
  51.         printf("inet_pton error!\n");
  52.         return -1;
  53.     };
  54. #endif

  55.     if (connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
  56.     {
  57.         printf("Connect failed... \n");
  58.         return -1;
  59.     }
  60.     printf("Connect to %s.... \n", IPSTR);
  61.      
  62.     memset(buffer, 0, sizeof(buffer));
  63.      
  64.     if(recv(sockfd, buffer, sizeof(buffer), 0) < 0)
  65.     {
  66.         printf("receive failed... \n");
  67.         return -1;
  68.     }
  69.     else
  70.     {
  71.          printf("%s\n", buffer);
  72.     }
  73.     return sockfd;
  74. }
  75.  

  76. int getResponse()
  77. {
  78.     memset(buffer,0,sizeof(buffer));
  79.     ret = recv(sockfd,buffer,1024,0);
  80.     if(ret == SOCKET_ERROR)
  81.     {
  82.         printf("receive nothing\n");
  83.         return -1;
  84.     }
  85.     buffer[ret]='\0';
  86.      
  87.     if(*buffer == '5')
  88.     {
  89.         printf("the order is not support smtp host\n ");
  90.         return -1;
  91.     }

  92.     printf("recieved buffer:%s\n",buffer);

  93.     return 0;
  94. }
  95.  
  96. int login(char *username,char *password)
  97. {
  98.     printf("login.....sockfd: %d\n",sockfd);
  99.     char ch[100];
  100.     if(username == "" || password == "")
  101.         return -1;
  102.      
  103.     send_data = "HELO 163.com\r\n";
  104.     printf("%s\n",send_data);
  105.     ret = send(sockfd, send_data, strlen(send_data), 0);
  106.     if(ret == SOCKET_ERROR)
  107.     {
  108.         close(sockfd);
  109.         return -1;
  110.     }
  111.     if(getResponse() < 0)
  112.         return -1;
  113.      
  114.     send_data = "AUTH LOGIN\r\n";
  115.     printf("%s\n",send_data);
  116.     ret = send(sockfd, send_data, strlen(send_data), 0);
  117.     if(ret == SOCKET_ERROR)
  118.     {
  119.         close(sockfd);
  120.         return -1;
  121.     }
  122.     if(getResponse() < 0)
  123.         return -1;
  124.     sprintf(ch, "%s\r\n", username);
  125.     printf("%s\n",ch);
  126.     ret = send(sockfd, (char *)ch, strlen(ch),0);
  127.     if(ret == SOCKET_ERROR)
  128.     {
  129.         close(sockfd);
  130.         return -1;
  131.     }
  132.      
  133.     if(getResponse() < 0)
  134.          return -1;
  135.      
  136.     sprintf(ch,"%s\r\n",password);
  137.     printf("%s\n",ch);
  138.     ret = send(sockfd,(char *)ch,strlen(ch),0);
  139.     if(ret == SOCKET_ERROR)
  140.     {

  141.         close(sockfd);
  142.         return -1;
  143.     }
  144.      
  145.     if(getResponse() < 0)
  146.         return -1;
  147.      
  148.     return 0;
  149.  
  150. }
  151.  
  152. int sendmail(char* from, char * to, char * subject, char * context)
  153. {
  154.     if(from =="" ||to == ""||subject == ""||context == "")
  155.     {
  156.         printf("arguments error!\n");
  157.         return -1;
  158.     }
  159.     
  160.     sprintf(From, "MAIL FROM: <%s>\r\n", from);
  161.     printf("%s\n",From);
  162.      
  163.     if((ret = send(sockfd, From, strlen(From), 0)) == SOCKET_ERROR)
  164.     {
  165.         close(sockfd);
  166.         return -1;
  167.     }
  168.      
  169.     if(getResponse() < 0)
  170.         return -1;
  171.      
  172.     sprintf(To, "RCPT TO: <%s>\r\n", to);
  173.     if((ret = send(sockfd, To, strlen(To),0)) == SOCKET_ERROR)
  174.     {
  175.         close(sockfd);
  176.         return -1;
  177.     }
  178.     if(getResponse() < 0)
  179.         return -1;
  180.      
  181.     send_data = "DATA\r\n";
  182.     if((ret = send(sockfd,send_data,strlen(send_data),0)) == SOCKET_ERROR)
  183.     {
  184.         close(sockfd);
  185.         return -1;
  186.     }
  187.     if(getResponse() < 0)
  188.         return -1;
  189.         
  190.     memset(Subject, 0, sizeof(Subject));
  191.     sprintf(Subject, "Subject: %s\r\n", subject);
  192.     strcat(Context, Subject);
  193.     strcat(Context, "\r\n\r\n");
  194.     //strcat(Context,context);
  195.     strcat(Context,"\r\n.\r\n");
  196.     if((ret= send(sockfd, Context, strlen(Context), 0)) == SOCKET_ERROR)
  197.     {
  198.         close(sockfd);
  199.         return -1;
  200.     }
  201.      
  202.     printf("ret size:%d\n", ret);
  203.     
  204.     memset(buffer, '\0', sizeof(buffer));
  205.     
  206.     if(getResponse() < 0)
  207.          return -1;
  208.  
  209.     printf("QUIT!!!!!!!!!!!!!!!!\n");
  210.     if((ret = send(sockfd,"QUIT\r\n",strlen("QUIT\r\n"), 0)) == SOCKET_ERROR)
  211.     {
  212.         close(sockfd);
  213.         return -1;
  214.     }
  215.     if(getResponse() < 0)
  216.         return -1;
  217.  
  218.     printf("Send Mail Successful!\n");
  219.     return 0;
  220. }
  221.  

  222. int main()
  223. {
  224.     char from[128] = "funnylinux@163.com";
  225.     char to[128] = "754056915@qq.com";

  226.     char subject[512] = "test....";
  227.     char context[6000] = "Just a test.........\n";
  228.     char server[56]="smtp.163.com";

  229.     char *name ="ZnVubnlsaW51eEAxNjMuY29t";//base64
  230.     char *passwd = "YWk1MjQwMzI=";//base64
  231.  
  232.     if(connectHost(server)<0)
  233.     {
  234.         printf("Can Not LOGIN !\n");
  235.         return -1;
  236.     }
  237.  
  238.     if(login(name, passwd) < 0)
  239.     {
  240.         fprintf(stderr,"Can Not LOGIN !\n");
  241.         return -1;
  242.     }
  243.     sendmail(from, to, subject, context);
  244.     return 0;
  245.  }


阅读(3000) | 评论(0) | 转发(1) |
0

上一篇:STM32_ADE7758驱动

下一篇:Wubi安装Ubuntu

给主人留下些什么吧!~~