------------------------------------ pop3.c ------------------------------------
/*-------------------------------------------------------------------------------------------
注释: 通过程序对互联网上的一个邮箱进行操作,读取邮箱里面的内容,本例读的内容是邮件标题中的IP和PORT
[root@localhost pop3]# gcc -o pop3 pop3.c
--------------------------------------------------------------------------------------------*/
/* GNU GPLv2 only or any later version. */
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char *POPMessage[] = {
"USER mailboxname\r\n",
"PASS 123456\r\n",
NULL
};
char buf[5000];
int i, n;
int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12;
int sockfd, new_fd, iLength;
struct sockaddr_in server_addr;
struct sockaddr_in servaddr;
//struct sockaddr_in client_addr;
//int sin_size;
int iMsg = 0;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(" socket error");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(110);
server_addr.sin_addr.s_addr = inet_addr("123.125.50.23");
bzero(&(server_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) {
perror(" connect error");
exit(1);
}
iLength = recv(sockfd, buf, sizeof(buf), 0);
buf[iLength] = '\0';
printf("received:%s\n", buf);
do {
send(sockfd, POPMessage[iMsg], strlen(POPMessage[iMsg]), 0);
printf("发送帐号:%s", POPMessage[iMsg]);
memset(buf, '\0', 1024);
iLength = recv(sockfd, buf, sizeof(buf), 0);
buf[iLength] = '\0';
iMsg++;
printf("接收信息:%s\n", buf);
} while (POPMessage[iMsg]);
send(sockfd, "RETR 1\r\n", strlen("RETR 1\r\n"), 0);
memset(buf, '\0', 5000);
//开始接收邮件
num1 = num2 = num3 = 0;
n = 0;
int k = 0, j, x = 0;
char a[2], *token, b = 20;
for (i = 0; i < 5000; i++) {
n++;
recv(sockfd, a, 1, 0);
if (strcmp(a, "S") == 0)
num1 = n;
if (strcmp(a, "u") == 0)
num2 = n;
if (strcmp(a, "b") == 0)
num3 = n;
if (strcmp(a, "j") == 0)
num4 = n;
if (strcmp(a, "e") == 0)
num5 = n;
if (strcmp(a, "c") == 0)
num6 = n;
if (strcmp(a, "t") == 0)
num7 = n;
if (strcmp(a, ":") == 0)
num8 = n;
if (strcmp(a, "m") == 0)
num9 = n;
if (strcmp(a, "y") == 0)
num10 = n;
if (strcmp(a, "q") == 0)
num11 = n;
if (strcmp(a, "q") == 0)
num12 = n;
if (((num12 - num1) == 11) && ((num10 - num9) == 1) && ((num8 - num7) == 1) && ((num6 - num5) == 1) && ((num4 - num3) == 1) && ((num2 - num1) == 1)) {
for (j = 0; j < 45; j++) {
recv(sockfd, a, 1, 0);
strcat(buf, a);
x = 1;
}
}
if (x)
break;
}
token = strtok(buf, ":");
token = strtok(NULL, ":");
printf("想要的ip是:\n%s\n", token);
token = strtok(NULL, ":");
printf("想要的port是:\n%s\n", token);
close(sockfd);
}
/*------------------------------------------------------------------------------------
执行过程如下:
[root@localhost pop3]# ./pop3
received:+OK Welcome to coremail Mail Pop3 Server (126coms[f7aa0ed0270c139a0807240dd700906bs])
发送帐号:USER mailboxname
接收信息:+OK core mail
发送帐号:PASS 123456
接收信息:+OK 1 message(s) [989 byte(s)]
想要的ip是:
127.123.123.123
想要的port是:
8000
[root@localhost pop3]#
-------------------------------------------------------------------------------------*/