C language Check native public IP and send mail

Source: Internet
Author: User
Tags base64 readfile email account

This is a use to get the local public IP address, and check whether it is the IP address saved in the configuration, if not, send a message to the specified mailbox, report this IP address a small piece of code. Put on the boot, the computer does not set password when lost, but also can have a thought .... function is not strong-just simple to get the address, the ability to send mail, only for practice, and only 163 of the mailbox to send mail ...

The function of sending mail is Baidu Copy, just simple change a bit

#include <windows.h> #include <stdio.h> #include <WinSock.h> #include <urlmon. h> #include <FSTREAM> #include <iostream> #define N 500using namespace std; #pragma comment (lib, "ws2_32. Lib ") #pragma comment (lib," Urlmon.lib ") struct base64date6{unsigned int d4:6;unsigned int d3:6;unsigned int d2:6; unsigned int d1:6;};/ The encryption section of the protocol is using the Base64 method Char ConvertToBase64 (char c6), void EncodeBase64 (char *dbuf, char *buf128, int len), void Se Ndmail (char *emailto,char *emailfrom,char *emailpd,char *body); int opensocket (struct sockaddr *addr); in T Checkip (char currentip[])//0 failed to read file, 1IP untrusted 2 download file failed 3 Trusted IP address {HRESULT hr=urldownloadtofile (0, "http://www.ip138.com/i Ps1388.asp "," CurrentIP.txt ", 0,null); if (HR!=S_OK) {return 2;} Ifstream readFile ("IPlist.txt"); if (!readfile) {cout<< "Can ' t find IPlist.txt" <<endl;return 0;} Char listip[n];readfile.getline (listip,n, ' \ n '); Readfile.close (); Readfile.clear (); Readfile.open ("CurrentIP.txt") ; if (!readfile) {cout<< "Can ' t find CurrentIP.txt" <<endl;return 0;}  Char Readtemp[n*4];while (!readfile.eof ()) {Readfile.getline (readtemp,n*4, ' \ n '); Getline function Parameter Description: Reference 1--> refers to the needle, the number of parameters 2--> the maximum read value, the number of 3--> read Terminator if (Readfile.fail ()) Break;else {char *temp=strstr ( Readtemp, "Your IP address is"), if (temp!=null) {TEMP=STRCHR (temp, ' ['); for (int i=0;; i++) {if (temp[i+1]== '] ') {currentip[i]= '}; ELSECURRENTIP[I]=TEMP[I+1];} Break;}}} Readfile.close (); Readfile.clear (); Char *p=strstr (LISTIP,CURRENTIP); if (p!=null) {cout<< "Been trusted!" <<endl;return 3;} else {cout<< "Unknown address! Send e-Mail immediately! " <<endl;return 1;}} int main () {char currentip[16];int n=0,ir;while (1) {ir=checkip (Currentip); if (ir==2) {if (n!=10) {cout<< " Connection failed ... It ' s too bad! I'll try again one minute later. " <<endl;n++; Sleep (60*1000);} else{cout<< "Connection failed for ten times. Application'll exit immediately! " <<endl;return 0;}} else break;} if (ir==0) {cout<< "Read file error! Application wilL Exit immediately! " <<endl;return 0;} else if (ir==3) {cout<< "Been trusted! Have fun! " <<endl;return 0;} else {char Emailto[100];char emailfrom[100];char emailpd[100];fstream readFile ("Email.ini"); if (!readfile) {cout< < "Can ' t Open Email.ini" <<endl;return 0;}  while (!readfile.eof ()) {Readfile.getline (emailto,100, ' \ n '); Getline function Parameter Description: The reference 1--> refers to the needle, the number of parameters 2--> the maximum read value, the number of 3--> read Terminator Readfile.getline (emailfrom,100, ' \ n '); Readfile.getline (emailpd,100, ' \ n ');} Readfile.close (); Readfile.clear (); char emailcontents[n] = "From: \" yourpc\ "<[email protected]>\r\n" "to: \ "Mymaster\" <[email protected]>\r\n "" Subject:ip address has been changed!\r\n\r\n "; Strcat (emailcontents , "The address is:"); strcat (Emailcontents,currentip); Strcat (emailcontents, "!!! Please check! "); SendMail (emailto,emailfrom,emailpd,emailcontents); return 0;}} Char ConvertToBase64 (char UC) {if (UC < +) {return ' A ' + UC;} if (UC < n) {return ' a ' + (UC-26);} if (UC < +) {return ' 0 ' + (uc-52);} IfUC = = +) {return ' + ';} return '/';}        Base64 implementation of void EncodeBase64 (char *dbuf, char *buf128, int len) {struct Base64date6 *ddd = Null;int I = 0;char buf[256] = {0};char *tmp = null;char cc = ' n '; memset (buf, 0, N); strcpy_          S (buf, N, 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 in T) ddd->d1);d buf[(i-1) *4+1] = ConvertToBase64 ((unsigned int) ddd->d2);d buf[(i-1) *4+2] = ConvertToBase64 (( unsigned int) ddd->d3);d buf[(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;dd D = (struct Base64date6 *) tmp;dbuf[(i-1) *4+0] = ConvertToBase64 ((unsigned int) ddd->d1);d buf[(i-1) *4+1] = Co NvertToBase64 ((unsigned int) ddd->d2);d buf[(i-1) *4+2] = ' = ';d buf[(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);d buf[(i-1) *4+1] = Conver TToBase64 ((unsigned int) ddd->d2);d buf[(i-1) *4+2] = ConvertToBase64 ((unsigned int) ddd->d3);d buf[(i-1) *4+3] = ' = ‘;} return;}  Send message 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 (+), 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); Connect to mail server, assuming that there is no response after 2 seconds, then connect SOCKFD = Opensocket ((struct sockaddr *) &their_addr), memset (rbuf, 0,); Recv (SOCKFD, rbuf, 0) = = 0) {cout<< "Reconnect ..." <<endl; Sleep (sockfd = opensocket (struct sockaddr *) &their_addr) memset (rbuf, 0, 1500);} cout<<rbuf<<endl;//Ehlomemset (buf, 0, sprintf_s); buf, EHLO, "hyl-pc\r\n sockfd"); Send (buf Strlen (BUF), 0); memset (rbuf, 0, 0); recv (SOCKFD, Rbuf,);cout<< "EHLO REceive:" <<rbuf<<endl ;//AUTH Loginmemset (buf, 0, sprintf_s); BUF (AUTH, login\r\n, "sockfd buf"), Send (strlen,, buf (memset), 0); 0, recv (sockfd, rbuf, 0);cout<< "Auth Login Receive:" <<rbuf<<endl;//usermemset (buf, 0, sprintf_s (buf, emailfrom);//Your email account memset (login, 0, 128); EncodebAse64 (login, buf, strlen (BUF)), sprintf_s (buf,, "%s\r\n", login); Send (SOCKFD, buf, strlen (BUF), 0);cout<< " Base64 UserName: "<<buf<<endl;memset (rbuf, 0,) recv (SOCKFD, Rbuf,, 0);cout<<" User Login Receive: "<<rbuf<<endl;//passwordsprintf_s (buf, EMAILPD);//Your mailbox Passwordmemset (pass, 0, 128); EncodeBase64 (pass, buf, strlen (BUF)), sprintf_s (buf,, "%s\r\n", pass); Send (SOCKFD, buf, strlen (BUF), 0);cout< < "Base64 Password:" <<buf<<endl;memset (rbuf, 0,) recv (SOCKFD, rbuf, 0);cout<< "Send Password Receive: "<<rbuf<<endl;//mail Fromchar mailfrom[100]=" mail from: < "strcat (Mailfrom, strcat (Mailfrom, ">\r\n") memset (buf, 0, Emailfrom) sprintf_s (buf, Mailfrom, SOCKFD); Send (buf BUF), 0); memset (rbuf, 0, 0); recv (SOCKFD, Rbuf,);cout<< "set Mail from Receive:" <<rbuf<<endl ;//RCPT to the first recipient sprintf_s (BUF, RCPT to:<%s>\r\n, Emailto); Send (soCKFD, buf, strlen (BUF), 0), memset (rbuf, 0, 0), recv (SOCKFD, Rbuf,);cout<< "Tell Sendto Receive:" <<r buf<<endl;//DATA prepares to send mail content sprintf_s (buf, data\r\n); Send (SOCKFD, buf, strlen (BUF), 0); memset (rbuf, 0, Recv (SOCKFD, rbuf, 0);cout<< "Send mail Prepare Receive:" <<rbuf<<endl;//send mail content, \r\n.\r\ n the end of content tag sprintf_s (buf, "%s\r\n.\r\n", "body"), Send (SOCKFD, buf, strlen (BUF), 0), memset (rbuf, 0,); Recv (SOCKFD, Rbuf, 0);cout<< "Send Mail Receive:" <<rbuf<<endl;//quitsprintf_s (buf,, "quit\r\n"); Send ( SOCKFD, buf, strlen (BUF), 0), memset (rbuf, 0, 0), recv (SOCKFD, Rbuf,);cout<< "Quit Receive:" <<rbuf& lt;<endl;//cleanup Work closesocket (SOCKFD); WSACleanup (); return;} Open the TCP socket connection int opensocket (struct sockaddr *addr) {int sockfd = 0;sockfd=socket (pf_inet, sock_stream, 0); if (SOCKFD &L T 0) {cout<< "Open sockfd (TCP) error!" <<endl;exit (-1);} if (Connect (sockfd, addr, sizeof (struct SOCKADDR)) < 0) {cout<< "Connect sockfd (TCP) error!" <<endl;exit (-1);}  return SOCKFD;}

Attached resources: http://download.csdn.net/detail/yangyangsnr/7367321.

C language Check native public IP and send mail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.