POP3Dlg.cpp

 

http://read.pudn.com/downloads2/sourcecode/windows/internet/3619/POP3/Copy%20of%20POP3Dlg.cpp__.htm

// POP3Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "POP3.h"
#include "POP3Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define POPOK "+OK"
#define POPERR "-ERR"
SOCKET m_hSocket;

SOCKET tcp_connect(const char* hostname);
int pop3resp(char *buf, int size);
int pop3retr(char *buf, int size);
int pop3readbody(char *buf, int size);
int pop3readmail();

int readsock(char *buf, int size);
int pop3readline();
int base64_decode(char *pSrc, unsigned int nSize, char *pDest);

/
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CPOP3Dlg dialog

CPOP3Dlg::CPOP3Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CPOP3Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPOP3Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPOP3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPOP3Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPOP3Dlg, CDialog)
//{{AFX_MSG_MAP(CPOP3Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDMAIL, OnMail)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CPOP3Dlg message handlers

BOOL CPOP3Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX &amt; 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CPOP3Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID &amt; 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CPOP3Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&amt;rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

HCURSOR CPOP3Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CPOP3Dlg::OnMail()
{
// TODO: Add your control notification handler code here

//char hostname[] = "szpop.utstar.com.cn";
//char username[] = "shaozl";
//char password[] = "lszisgood";

char hostname[] = "pop3.netease.com";
char username[] = "lszisgood";
char password[] = "a9506229";

char buf[1024];
int i,mailnum=0;

m_hSocket = tcp_connect(hostname);
if(m_hSocket<0)
return;
pop3resp(buf,1024);

sprintf(buf, "USER >s/r/n", username);
send(m_hSocket, buf, strlen(buf), 0);
//write(sockfd, buf, strlen(buf));
pop3resp(buf,1024);

sprintf(buf,"PASS >s/r/n", password);
send(m_hSocket, buf, strlen(buf), 0);
//write(sockfd, buf, strlen(buf));
pop3resp(buf,1024);

if (strncmp(buf, POPERR, strlen(POPERR)) == 0)
{
return;
}

strcpy(buf,"STAT/r/n");
send(m_hSocket, buf, strlen(buf), 0);
//write(sockfd, buf, strlen(buf));
pop3resp(buf,1024);

strtok(buf, " ");
mailnum = atoi(strtok(NULL, " "));
//octets = atoi(strtok(NULL, " "));
for (i = 1; i <= mailnum; i++)
{
sprintf (buf,"RETR >d/r/n",i);
send(m_hSocket, buf, strlen(buf), 0);
//write(sockfd, buf, strlen(buf));
pop3readmail();
}

sprintf (buf,"QUIT/r/n",i);
send(m_hSocket, buf, strlen(buf), 0);
//write(sockfd, buf, strlen(buf));

pop3resp(buf,1024);
closesocket(m_hSocket);
}

SOCKET tcp_connect(const char* hostname)
{
struct sockaddr_in addr;
struct hostent *he = gethostbyname(hostname);
//int sockfd;
SOCKET sockfd;

if (!he)
{
return(-1);
}

memset((char *)&amt;addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
memcpy(&amt;(addr.sin_addr.s_addr), he->h_addr, he->h_length);
addr.sin_port = htons(110);

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket()");
return(-1);
}

if (connect(sockfd, (struct sockaddr *)&amt;addr, sizeof(struct sockaddr)) < 0)
{
closesocket(sockfd);
perror("connect()");
return(-1);
}
return(sockfd);
}

/* receive one line server responses terminated with CRLF */
int pop3resp(char *buf, int size)
{
char *pos;
int bytes = 0;
memset(buf, 0, size);
while ((pos = strstr(buf, "/r/n")) == NULL)
// bytes += read(sockfd, buf+bytes, size-bytes);
bytes += recv(m_hSocket, buf+bytes, size, 0);
*(pos++) = '/0';
return bytes;
}

int pop3retr(char *buf, int size)
{
FILE *fp;
char filename[128];
static int fileindex=1;
int bytes=0;

//pop3resp(buf,1024);
memset(buf, 0, 1024);
bytes = recv(m_hSocket, buf,1020, 0);

if (strnicmp(buf,"-ERR", 4) == 0)
{
return -1;
}
sprintf(filename,"e:/mymail>d.txt",fileindex++);
fp = fopen(filename,"wt");
if(!fp)
return -2;

fprintf(fp,buf);

while(1)
{
memset(buf, 0, 1024);
bytes = recv(m_hSocket, buf,1020, 0);
fprintf(fp,buf);
if((buf[bytes-3]=='.')&amt;&amt;
(buf[bytes-2]=='/r')&amt;&amt;
(buf[bytes-1]=='/n')||
bytes==0
)
break;
}
fflush(fp);
fclose(fp);
return 0;
}

int pop3readbody(char *buf, int size)
{
char *inbuf,*pos;
char Content[] = "Content-Transfer-Encoding: base64";
int contentlen= strlen(Content);
int bytes=0;
int readbytes=0;

FILE *fp;

fp = fopen("e:/mylog.txt","wt");

memset(buf, 0, 300);
bytes = recv(m_hSocket, buf,1020, 0);

if (strnicmp(buf,"-ERR", 4) == 0)
{
return -1;
}

inbuf = buf+bytes;
readbytes = bytes;
while(1)
{
if ((pos = strstr(buf,Content)) != NULL)
{
readbytes = readbytes-(pos-buf+contentlen);
memmove(buf,(pos+contentlen),readbytes);
inbuf = buf + readbytes;
memset(inbuf, 0, 1024-readbytes);
break;
}
if(readbytes>=300)
{
fprintf(fp,buf);
fprintf(fp,"/n-------------------/n");
memmove(buf,(buf+readbytes-contentlen),contentlen);
readbytes = contentlen;
inbuf = buf + contentlen;
memset(inbuf, 0, 1024-contentlen);
}

bytes = recv(m_hSocket, inbuf,300-readbytes, 0);

if((inbuf[bytes-3]=='.')&amt;&amt;
(inbuf[bytes-2]=='/r')&amt;&amt;
(inbuf[bytes-1]=='/n')||
bytes==0
)
return 2;

readbytes += bytes;
inbuf = buf+readbytes;
}

while(readbytes<1000)
{
bytes = recv(m_hSocket, inbuf,1000-readbytes, 0);

if((inbuf[bytes-3]=='.')&amt;&amt;
(inbuf[bytes-2]=='/r')&amt;&amt;
(inbuf[bytes-1]=='/n')||
bytes==0
)
return 2;

readbytes += bytes;
inbuf = buf+readbytes;
}

fprintf(fp,"/n<------------------->>/n");

fprintf(fp,buf);
fflush(fp);

if ((pos = strstr(buf,"filename=")) == NULL)
{
return 3;
}

fprintf(fp,"/n<------------------->>/n");

fprintf(fp,pos);

fclose(fp);

return 0;
}


int pop3readmail()
{
char bigbuf[8192];
char *inbuf,*pos;
char Content[] = "Content-Transfer-Encoding: base64";
char filename[28]="";

memset(bigbuf,0,8192);

memset(filename,0,28);

FILE *fp;
fp = fopen("e:/mylog.txt","wt");

while(1)
{
int ret = readsock(bigbuf,8192);
if(ret == -1)
break;

if ((pos = strstr(bigbuf,Content)) != NULL)
{
break;
}
}
pos = bigbuf;
while(pos!=NULL)
{
if ((pos = strstr(pos,Content)) != NULL)
{
pos = strstr(pos,"filename=");
//pos = strstr(pos,"/r/n/r/n");
}
if(pos)
{
pos+=10;
int i=0;
while(*pos!='"' &amt;&amt; i<20)
filename[i++] = *pos++;

pos = strstr(pos,"/r/n/r/n");
pos +=2;

fprintf(fp,filename);
fprintf(fp,pos);
}
}

fclose(fp);

return 0;
}

char socketbuf[8192];
int totallen=0;
int pos = 0;
int endflag=0;

int readline(char* linebuf,int size)
{
int readbytes=0;
int bytes=0;
int index=0;

linebuf[size] = '/0';
linebuf[size+1] = '/0';

if(totallen<=0)
{
totallen = readsock(socketbuf,8192);
pos =0;
}

while(size>0)
{
bytes = totallen - pos;

readbytes = bytes>size?size:bytes;

while(index<readbytes)
{
linebuf[index++] = socketbuf[pos++];
if(socketbuf[pos]=='/r' &amt;&amt; socketbuf[pos+1] == '/n')
{
size = 0;
break;
}
}
if(readbytes<size &amt;&amt; !endflag)
{
totallen = readsock(socketbuf,8192);
pos =0;
}
size -= readbytes;
if(totallen==pos &amt;&amt; endflag)
break;
}
linebuf[index] = '/0';
return index;
}

int readsock(char *buf, int size)
{
char *inbuf=buf;
int readbytes=0;
int bytes=0;

while(readbytes<size&amt;&amt; !endflag)
{
bytes = recv(m_hSocket, inbuf,size-readbytes, 0);

if((inbuf[bytes-3]=='.')&amt;&amt;
(inbuf[bytes-2]=='/r')&amt;&amt;
(inbuf[bytes-1]=='/n')||
bytes==0
)
endflag = 1;

readbytes +=bytes;
inbuf +=bytes;
}
return readbytes;
}

int pop3readline()
{
char* pos=NULL;
char linebuf[300];
char outbuf[300];
int status=1;
int bytes=1;
int index=0;
char filename[128];
FILE *fp;

while(bytes)
{
bytes = readline(linebuf,256);
if(!bytes)
break;

switch(status)
{
case 1:
if(strstr(linebuf,"Encoding: base64")!=NULL)
{
status = 2;
}
break;
case 2:
if((pos=strstr(linebuf,"filename="))!=NULL)
{
status = 3;
pos+=10;
index=0;
memset(filename,0,128);
while(*pos!='"' &amt;&amt; index<20)
filename[index++] = *pos++;
}
break;
case 3:
if(strcmp(linebuf,"/r/n")==0)
{
status = 4;
sprintf(outbuf,"e:/>s",filename);
fp = fopen(outbuf,"wb");
if(!fp)
status = 1;
}
break;
case 4:
if(bytes==2)
{
status = 1;
fclose(fp);
}else
{
memset(outbuf,0,256);
index = base64_decode(linebuf,bytes,outbuf);
fwrite(outbuf,index,1,fp);
}
break;
}
}
return 0;
}

//------------------------------------------------------------------------------
//Base64解码
//Base64编码表
const unsigned int BASE64_DECODE_TABLE[256] =
{
255, 255, 255, 255, 255, 255, 255, 255, // 00 - 07
255, 255, 255, 255, 255, 255, 255, 255, // 08 - 15
255, 255, 255, 255, 255, 255, 255, 255, // 16 - 23
255, 255, 255, 255, 255, 255, 255, 255, // 24 - 31
255, 255, 255, 255, 255, 255, 255, 255, // 32 - 39
255, 255, 255, 62, 255, 255, 255, 63, // 40 - 47
52, 53, 54, 55, 56, 57, 58, 59, // 48 - 55
60, 61, 255, 255, 255, 255, 255, 255, // 56 - 63
255, 0, 1, 2, 3, 4, 5, 6, // 64 - 71
7, 8, 9, 10, 11, 12, 13, 14, // 72 - 79
15, 16, 17, 18, 19, 20, 21, 22, // 80 - 87
23, 24, 25, 255, 255, 255, 255, 255, // 88 - 95
255, 26, 27, 28, 29, 30, 31, 32, // 96 - 103
33, 34, 35, 36, 37, 38, 39, 40, // 104 - 111
41, 42, 43, 44, 45, 46, 47, 48, // 112 - 119
49, 50, 51, 255, 255, 255, 255, 255, // 120 - 127
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255
};

int base64_decode(char *pSrc, unsigned int nSize, char *pDest)
{
if ((pSrc == NULL) || (pDest == NULL) || (nSize <= 0))
return 0;

unsigned int lByteBuffer, lByteBufferSpace;
unsigned int C; //临时阅读变量
int reallen;
char *InPtr, *InLimitPtr;
char *OutPtr;

lByteBuffer = 0; lByteBufferSpace = 4;

InPtr = pSrc;
InLimitPtr= InPtr + nSize;
OutPtr = pDest;

while (InPtr != InLimitPtr)
{
C = BASE64_DECODE_TABLE[*InPtr]; // Read from InputBuffer.
InPtr++;
if (C == 0xFF) continue; //读到255非法字符
lByteBuffer = lByteBuffer << 6 ;
lByteBuffer = lByteBuffer | C ;
lByteBufferSpace--;
if (lByteBufferSpace != 0) continue; //一次读入4个字节
//到序写入3个字节到缓冲
OutPtr[2] = lByteBuffer;
lByteBuffer = lByteBuffer >> 8;
OutPtr[1] = lByteBuffer;
lByteBuffer = lByteBuffer >> 8;
OutPtr[0] = lByteBuffer;
//准备写入后3位
OutPtr+= 3; lByteBuffer = 0; lByteBufferSpace = 4;
}
reallen = (unsigned int)OutPtr - (unsigned int)pDest;
//处理尾部 返回实际长度
switch (lByteBufferSpace)
{
case 1:
lByteBuffer = lByteBuffer >> 2;
OutPtr[1] = lByteBuffer;
lByteBuffer = lByteBuffer >> 8;
OutPtr[0] = lByteBuffer;
return reallen + 2;
case 2:
lByteBuffer = lByteBuffer >> 4;
OutPtr[0] = lByteBuffer;
return reallen + 1;
default:
return reallen;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值