|
东陆风华帐号全站通用,包括论坛、商城、网络家园等站点,登陆查看更精彩内容
您需要 登录 才可以下载或查看,没有帐号?注册东陆风华通行证
x
放假的时候写的。。。。呵呵。。。。
参考资料:《HowTo.SMTP》,《SendMail》(NextFly写的),《MSDN 2005》
好了,废话就不说了。下面是源代码:
- #include <stdio.h>
- #include <stdlib.h>
- #include <winsock2.h>
- #pragma comment(lib, "ws2_32.lib")
- const char *MailData = "From: "88250"<dl88250@126.com>\r\n"
- "Subject: This is only a test mail! ^^\r\n.\r\n";
- int main(int argc, char *argv[])
- {
-
- WSADATA wsaData;
- WORD wVersionRequested = MAKEWORD(2, 2);
- struct hostent *pHostent = NULL;
- SOCKET server = INVALID_SOCKET;
- struct sockaddr_in service;
- int retConnect = 0;
- char Buffer[1024] = {0};
-
- if(WSAStartup(wVersionRequested, &wsaData) != 0){
- printf("Error at WSAStartup()\n");
- goto WSACleanup;
- }
-
- server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create a Socket
- if(server == INVALID_SOCKET){
- printf("Error at socket(): %ld\n", WSAGetLastError());
- goto WSACleanup;
- }
-
- pHostent = gethostbyname("smtp.126.com"); //Get the Mail Server Name
- if(pHostent == NULL){
- printf("The Host Name is Invalid...\n");
- goto WSACleanup;
- }
-
- service.sin_family = AF_INET;
- memcpy(&service.sin_addr.s_addr, pHostent->h_addr_list[0], pHostent->h_length);
- service.sin_port = htons(25);
- //Connect to the remote Mail Server
- retConnect = connect(server, (struct sockaddr*)&service, sizeof(service));
- if(retConnect == SOCKET_ERROR){
- printf("Failed to connect.\n");
- goto WSACleanup;
- }
-
- printf("Connect to %s....\n", inet_ntoa(service.sin_addr));
-
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Failed to connect.\n");
- goto WSACleanup;
- }else{
- printf("%s\n", Buffer);
- }
-
- //Send "HELO Server....\r\n" to the Mail Server
- retConnect = send(server, "HELO Server....\r\n", strlen("HELO Server....\r\n"), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send HELO to the Mail Failure.\n");
- goto WSACleanup;
- }else{
- printf("HELO Server....\n");
- }
-
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Failed to connect.\n");
- goto WSACleanup;
- }else{
- printf("%s\n", Buffer);
- }
- //Send "AUTH LOGIN\r\n" to the Mail Server
- retConnect = send(server, "AUTH LOGIN\r\n", strlen("AUTH LOGIN\r\n"), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send "AUTH LOGIN" to Mail Failure.\n");
- goto WSACleanup;
- }else{
- printf("AUTH LOGIN\n");
- }
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Receive Data From Mail Server Failure.\n");
- goto WSACleanup;
- }else{
- printf("%s\n", Buffer);
- }
-
- //Send UserName to the Mail Server. The UserName is Encoded by Base64.
- retConnect = send(server, "bGJleW9uZDRrb21h==\r\n", strlen("bGJleW9uZDRrb21h==\r\n"), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send UserName to the Mail Failure.\n");
- goto WSACleanup;
- }else{
- printf("UserName\n");
- }
-
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Receive Data From the Mail Server Failure.\n");
- goto WSACleanup;
- }else{
- printf("%s\n", Buffer);
- }
- //Send Password to the Mail Server The Password is Encoded by Base64.
- retConnect = send(server, "bGJleW9uZDRrb21h=\r\n", strlen("bGJleW9uZDRrb21h=\r\n"), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send Password to Mail Failure.\n");
- goto WSACleanup;
- }else{
- printf("Password\n");
- }
-
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Receive Data From the Mail Server Failure.\n");
- goto WSACleanup;
- }else{
- printf("%s\n", Buffer);
- }
- //Send "Mail From: " File to the Mail Server, sender's Mail Address
- retConnect = send(server, "MAIL FROM: <lbeyond4koma@126.com>\r\n", strlen("MAIL FROM: <lbeyond4koma@126.com>\r\n"), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send "Mail From: " to Mail Failure.\n");
- goto WSACleanup;
- }else{
- printf("MAIL FROM: <lbeyond4koma@126.com>\n");
- }
-
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Receive Data From Mail Server Failure.\n");
- goto WSACleanup;
- }else{
- Buffer[retConnect] = ' ';
- printf("%s\n", Buffer);
- }
- //Send "RCPT TO: " File to the Mail Server, receiver 's Mail Address
- retConnect = send(server, "RCPT TO: <dl88250@gmail.com>\r\n", strlen("RCPT TO: <dl88250@gmail.com>\r\n"), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send "RCPT TO: " to Mail Failure.\n");
- goto WSACleanup;
- }else{
- printf("RCPT TO: <dl88250@gmail.com>\n");
- }
-
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Receive Data From the Mail Server Failure.\n");
- goto WSACleanup;
- }else{
- printf("%s\n", Buffer);
- }
- //Send "Data" Fiele to the Mail Server, start to Send mail
- retConnect = send(server, "Data\r\n", strlen("Data\r\n"), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send "Data" Field to Mail Failure...\n");
- goto WSACleanup;
- }else{
- printf("Data\n");
- }
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Receive Data From Mail Server Failure...\n");
- goto WSACleanup;
- }else{
- printf("%s\n", Buffer);
- }
- //Send Mail data to the the Mail Server
- retConnect = send(server, MailData, strlen(MailData), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send Context Of Mail to Mail Failure...\n");
- goto WSACleanup;
- }else{
- printf("%s\n", MailData);
- }
- //Receive Data From the Mail Server
- ZeroMemory(Buffer, sizeof(Buffer));
- retConnect = recv(server, Buffer, sizeof(Buffer), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Receive Data From Mail Server Failure...\n");
- goto WSACleanup;
- }else{
- printf("%s\n", Buffer);
- }
- //Send "QUIT" Context to the Mail Server
- retConnect = send(server, "QUIT\r\n", strlen("QUIT\r\n"), 0);
- if(retConnect == SOCKET_ERROR){
- printf("Send "Quit" to Mail Failure...\n");
- goto WSACleanup;
- }else{
- printf("Quit\n");
- }
-
- printf("Send Mail Successful!\n");
-
- WSACleanup:{
- if(server != INVALID_SOCKET){
- closesocket(server);
- }
- WSACleanup();
- }
- system("pause");
- return 0;
- }
复制代码
其中需要注意的是寄件人地址与邮件内容里的邮件地址的关系:
寄件人的地址必须和你用的连接帐号一致。帐号和密码是使用Base64编码的(关于什么是Base64编码请到http://www.ynutx.net/blog/user1/Myth/archives/2006/1682.html下面看看,它的C实现代码在下面)。。。。
而在邮件内容里的地址只是一个告诉邮件服务器这封信是谁发的。可以和你所使用的发送帐号不一致!也就是说你可以伪造别人的地址进行邮件发送。。。。
哎,不要干什么干事哟^_^! 看邮件头是可以看出原始地址的哦:10: |
|