这是一个用来获取本机公网IP地址,并检查是否是配置里保存的IP地址,如果不是,就向指定的邮箱发送一个邮件,报告这个IP地址的一段小代码。放到开机启动中,电脑不设密码的时候万一丢了,还能有个念想..........功能不强——只简单实现了获取地址,发送邮件的功能,仅供练习之用,而且只能用163的邮箱来发邮件......
附上资源下载地址
具体下载目录在 /2014年资料/5月/19日/C语言检查本机公网IP并发送邮件
发送邮件的函数是百度上copy的,只是简单的修改了一下
#include <windows.h>
#include <stdio.h>
#include <WinSock.h>
#include <URLMON.H>
#include <FSTREAM>
#include <iostream>
#define N 500
using 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;
};
// 协议中加密部分使用的是base64方法
char ConvertToBase64 (char c6);
void EncodeBase64 (char *dbuf, char *buf128, int len);
void SendMail (char *emailTo,char *emailFrom,char *emailPd,char *body);
int OpenSocket (struct sockaddr *addr);
int CheckIP(char currentIP[]) //0读取文件失败,1IP非信任 2下载文件失败 3信任的IP地址
{
HRESULT hr=URLDownloadToFile(0,"http://www.ip138.com/ips1388.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函数参数说明:参数1-->指针头,参数2-->最大读取值,参数3-->读取结束符
if(readFile.fail()) break;
else
{
char *temp=strstr(readTemp,"您的IP地址是");
if(temp!=NULL)
{
temp=strchr(temp,'[');
for(int i=0;;i++)
{
if(temp[i+1]==']')
{
currentIP[i]='\0';
break;
}
else
currentIP[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 will try again one minute later."<<endl;
n++;
Sleep(60*1000);
}
else
{
cout<<"Connection failed for 10 times.Application will 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函数参数说明:参数1-->指针头,参数2-->最大读取值,参数3-->读取结束符
readFile.getline(emailFrom,100,'\n');
readFile.getline(emailPd,100,'\n');
}
readFile.close();
readFile.clear();
char EmailContents[N] = "From: \"YourPC\"<info@linuxidc.net>\r\n"
"To: \"MyMaster\"<root@linuxidc.net>\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;
}
}