您的位置:程序门 -> .net技术 -> vc.net



自己试着写了一个检测邮件的程序,但是无法和服务器连接。那位专家帮忙看一下,代码如下


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


自己试着写了一个检测邮件的程序,但是无法和服务器连接。那位专家帮忙看一下,代码如下
发表于:2008-03-15 23:16:39 楼主
程序可以运行但是连不上服务器,编译时不报错
#include "stdafx.h"
#include ".\pop3.h"
#include "mapi.h"
#define connection_check 1
#define user_check 2
#define password_check 3
#define stat_check 4

pop3::pop3(void)
:m_nnum(0)
,m_strerr(_t(""))
{
m_server.create();
}

pop3::~pop3(void)
{
m_server.close();
}
bool pop3::connect(void)
{
return 0;
}
int pop3::getnumofmail(void)
{
return m_nnum;
}
bool pop3::disconnect(void)
{
return 0;
}
bool pop3::connect(cstring&host,cstring&user,cstring&pwd)
{
char buf[512];
if(!m_server.connect(host,110))
{
m_strerr=_t("不能连接到邮件服务器");
return false;
}
else
{
if(checkresponse(connection_check)==false)
return false;
wsprintf(buf,"user%s\r\n",(lpcstr)user);
m_server.send(buf,(int)strlen(buf));
if(checkresponse(user_check)==false)
return false;
wsprintf(buf,"pass%s\r\n",(lpcstr)user);
m_server.send(buf,(int)strlen(buf));
if(checkresponse(password_check)==false)
return false;
return true;
}
}
bool pop3::checkresponse(int resptype)
{
char buf[256]="";
m_server.receive(buf,sizeof(buf));
switch(resptype)
{
        case connection_check:
if(strnicmp(buf,"-err",4)==0)
{
m_strerr=_t("错误的连接!");
return false;
}
break;
case user_check:
if(strnicmp(buf,"-err",4)==0)
{
m_strerr=_t("错误的用户名!");
return false;
}
break;
case password_check:
if(strnicmp(buf,"-err",4)==0)
{
m_strerr=_t("错误的密码!");
return false;
}
break;
case stat_check:
if(strnicmp(buf,"-err",4)==0)
{
m_strerr=_t("命令错误!");
return false;
}
else
{
bool mailnum=true;
for(char* p=buf;*p!='\0';p++)
{
if(*p=='\t' ¦ ¦*p=='\0')
{
if(mailnum==true)
{
m_nnum=atoi(p);
mailnum=false;
}
else
{
m_nnum=atoi(p);
return true;
}
}
}
}
break;
}
return 0;
}
bool pop3::statistics(void)
{
char buf[512];
wsprintf(buf,"stat\r\n");
m_server.send(buf,(int)strlen(buf));
if(checkresponse(stat_check)==false)
return false;
else
return true;
}
cstring pop3::geterrmsg(void)
{
return m_strerr;
}

这是处理检测程序的类
void ccheckmaildlg::ontimer(uint nidevent)
{
// todo: 在此添加消息处理程序代码和/或调用默认值
    pop3 popserver;
killtimer(1);
if(popserver.connect(m_server,m_user,m_pwd)==false)
afxmessagebox(popserver.geterrmsg());
else if(popserver.statistics()==false)
afxmessagebox(popserver.geterrmsg());
else
{
m_num=popserver.getnumofmail();
updatedata(false);
settimer(1,m_nduration*1000,null);
}
     cdialog::ontimer(nidevent);
}
这是执行的函数
发表于:2008-03-18 09:02:561楼 得分:0
没人能帮个忙吗   -_-!
发表于:2008-03-18 09:39:362楼 得分:0
你的测试邮件服务器是什么?

光看代码能看出来错误么?
发表于:2008-03-18 11:46:323楼 得分:0
就是测试有没有新邮件,现在端口可以打开了但是不能和服务器建立连接
发表于:2008-03-19 10:00:434楼 得分:0
连接服务器失败?
那你应该用getlasterror看看是什么原因。
发表于:2008-03-19 16:35:445楼 得分:0
ok


快速检索

最新资讯
热门点击