1
2
#include
<
stdio.h
>
3
#include
<
sys
/
socket.h
>
4
#include
<
sys
/
types.h
>
5
#include
<
netinet
/
in
.h
>
6
#include
<
stdlib.h
>
7
#include
<
string
.h
>
8
9
int
main(
int
argc,
char
*
argv[])
10
{
11
int
sockfd
=
-
1
;
12
int
iconn
=
-
2
;
13
ssize_t retConnect
=
-
2
;
14
struct
sockaddr_in servaddr;
15
char
ip[
20
]
=
"
192.168.0.251
"
;
//
設置SMTP地址
16
char
sentmsg[
2048
]
=
""
;
17
char
buf[
255
]
=
""
;
18
char
fileContent[
1024
]
=
""
;
19
FILE
*
file;
20
char
cin[
255
]
=
""
;
21
22
int
len
=
0
;
23
while
(
!
(argv[len]
==
NULL))
24
{
25
len
++
;
26
}
27
28
if
(len
!=
3
)
29
{
30
printf(
"
Usage:qmail yourname@163.com df.log\n
"
);
31
exit(
0
);
32
}
33
memset(fileContent,
'
\0
'
,
sizeof
(fileContent));
34
if
((file
=
fopen(argv[
2
],
"
r
"
))
==
NULL)
35
printf(
"
Not find file
"
);
36
else
37
{
38
while
(fgets(cin,
sizeof
(cin), file)
!=
NULL)
39
{
40
strcat(fileContent,cin);
41
memset(cin,
'
\0
'
,
sizeof
(cin));
42
}
43
}
44
fclose(file);
45
printf(
"
%s
"
,fileContent);
46
setvbuf(stdout,NULL,_IONBF,
0
);
47
sockfd
=
socket(AF_INET,SOCK_STREAM,
0
);
//
以scoket方式和郵件服務器通信
48
if
(sockfd
>
0
)
49
{
50
printf(
"
socket is open
"
);
51
bzero(
&
servaddr,
sizeof
(servaddr));
52
servaddr.sin_family
=
AF_INET;
53
servaddr.sin_port
=
htons(
25
);
54
inet_pton(AF_INET,ip,
&
servaddr.sin_addr);
55
iconn
=
connect(sockfd,(
struct
sockaddr
*
)
&
servaddr,
sizeof
(servaddr));
56
if
(iconn
==
0
)
57
{
58
printf(
"
connect to 192.168.0.251 25 success!
"
);
59
retConnect
=
recv(sockfd,buf,
sizeof
(buf),
0
);
60
if
(retConnect
==-
1
)
61
{
62
printf(
"
Failed to receive msg from smtp port
"
);
63
}
64
else
65
printf(
"
\nServer:%s\n
"
,buf);
66
memset(sentmsg,
'
\0
'
,
sizeof
(sentmsg));
67
strcpy(sentmsg,
"
HELO SERVER\r\n
"
);
68
retConnect
=
send(sockfd,sentmsg,strlen(sentmsg),
0
);
69
if
(retConnect
==-
1
)
70
{
71
printf(
"
\nFailed to send meg to smtp port in step 2.\n
"
);
72
exit(
1
);
73
}
74
else
75
{
76
printf(
"
%s
"
,sentmsg);
77
}
78
memset(buf,
'
\0
'
,
sizeof
(buf));
79
retConnect
=
recv(sockfd,buf,
sizeof
(buf),
0
);
80
if
(retConnect
==
-
1
)
81
{
82
printf(
"
\nFailed to recive meg from smtp port in step 3.\n
"
);
83
exit(
1
);
84
}
85
else
86
{
87
printf(
"
%s\n
"
,buf);
88
}
89
90
memset(sentmsg,
'
\0
'
,
sizeof
(sentmsg));
91
strcpy(sentmsg,
"
MAIL FROM: qdcm@163.com\r\n
"
);
92
strcat(sentmsg,
"
RCPT TO:
"
);
93
strcat(sentmsg,argv[
1
]);
94
strcat(sentmsg,
"
\r\n
"
);
95
retConnect
=
send(sockfd,sentmsg,strlen(sentmsg),
0
);
96
if
(retConnect
>
0
)
97
printf(
"
%s
"
,sentmsg);
98
memset(buf,
'
\0
'
,
sizeof
(buf));
99
retConnect
=
recv(sockfd,buf,
sizeof
(buf),
0
);
100
if
(retConnect
>
0
)
101
printf(
"
%s\n
"
,buf);
102
103
memset(sentmsg,
'
\0
'
,
sizeof
(sentmsg));
104
strcpy(sentmsg,
"
DATA\r\n
"
);
105
retConnect
=
send(sockfd,sentmsg,strlen(sentmsg),
0
);
106
if
(retConnect
>
0
)
107
printf(
"
%s
"
,sentmsg);
108
memset(buf,
'
\0
'
,
sizeof
(buf));
109
retConnect
=
recv(sockfd,buf,
sizeof
(buf),
0
);
110
if
(retConnect
>
0
)
111
printf(
"
%s\n
"
,buf);
112
113
memset(sentmsg,
'
\0
'
,
sizeof
(sentmsg));
114
strcpy(sentmsg,
"
From:qdcm@163.com\r\n
"
);
115
strcat(sentmsg,
"
To:
"
);
116
strcat(sentmsg,argv[
1
]);
117
strcat(sentmsg,
"
\r\n
"
);
118
strcat(sentmsg,
"
Subject:QDCM Host Check Data\r\n\r\n
"
);
119
strcat(sentmsg,fileContent);
120
strcat(sentmsg,
"
\r\n
"
);
121
strcat(sentmsg,
"
\r\n.\r\n
"
);
122
retConnect
=
send(sockfd,sentmsg,strlen(sentmsg),
0
);
123
memset(sentmsg,
'
\0
'
,
sizeof
(sentmsg));
124
125
strcpy(sentmsg,
"
QUIT\r\n
"
);
126
retConnect
=
send(sockfd,sentmsg,strlen(sentmsg),
0
);
127
if
(retConnect
>
0
)
128
printf(
"
%s
"
,sentmsg);
129
memset(buf,
'
\0
'
,
sizeof
(buf));
130
retConnect
=
recv(sockfd,buf,
sizeof
(buf),
0
);
131
if
(retConnect
>
0
)
132
printf(
"
%s\n
"
,buf);
133
134
}
135
else
136
{
137
printf(
"
connect 192.168.0.251 25 failed!
"
);
138
sleep(
1
);
139
}
140
close(sockfd);
141
}
142
else
143
{
144
printf(
"
open socket failed!
"
);
145
}
146
return
0
;
147
}
148