首先用VC做個pop3收郵件程序;不能連接;pop3的端口是110或995;
用telnet看一下能否收到郵箱郵件;這些都是不從瀏覽器進入郵箱收郵件的方式;telnet方式見此;
https://blog.csdn.net/bcbobo21cn/article/details/51321841
telnet也不能連接到郵箱;
一個VC通用的pop3類如下;
// pop3.cpp: implementation of the pop3 class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "pop.h"
#include "pop3.h"
#include "fstream.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
pop3::pop3()
{
m_PopServer.Create();
}
pop3::~pop3()
{
m_PopServer.Close();
}
BOOL pop3::Connect(CString & Host, CString & User, CString & Password)
{
char buf [512];
if (!m_PopServer.Connect(Host,110)) // 110 Pop3 Port
//if (!m_PopServer.Connect(Host,995)) // qq
{
m_ErrorMessage = _T("Server cannot be connected");
AfxMessageBox("Server cannot be connected!");
return FALSE;
}
else
{
if(CheckResponse(CONNECTION_CHECK)==FALSE)
{
AfxMessageBox("CAN NOT CONNECT TO SERVER!");
return FALSE;
}
wsprintf (buf, "USER %s\r\n", (LPCSTR) User);
m_PopServer.Send(buf, strlen (buf));
if(CheckResponse(USER_CHECK)==FALSE)
{
AfxMessageBox("WRONG USER!");
return FALSE;
}
wsprintf (buf, "PASS %s\r\n", (LPCSTR) Password);
m_PopServer.Send(buf, strlen (buf));
if (CheckResponse(PASSWORD_CHECK)==FALSE)
{
AfxMessageBox("WRONG PASSWORD!");
return FALSE;
}
return TRUE;
}
}
BOOL pop3::Delete(int & MsgNumber)
{
char buf [512];
wsprintf (buf, "DELE %d\r\n",MsgNumber );
m_PopServer.Send(buf, strlen (buf));
if (CheckResponse(DELETE_CHECK)==FALSE)
return FALSE;
else
return TRUE;
}
BOOL pop3::Disconnect()
{
char buf [512];
wsprintf (buf, "QUIT \r\n");
m_PopServer.Send(buf, strlen (buf));
if (CheckResponse(QUIT_CHECK)==FALSE)
return FALSE;
else
return TRUE;
}
BOOL pop3::Noop()
{
char buf [512];
wsprintf (buf, "NOOP \r\n");
m_PopServer.Send(buf, strlen (buf));
if (CheckResponse(NOOP_CHECK)==FALSE)
return FALSE;
else
return TRUE;
}
int pop3::GetMessageSize(int MsgNumber)
{
if(m_SizeOfMsg.GetSize() < MsgNumber+1)
return 0;
else
return m_SizeOfMsg[MsgNumber+1];
}
BOOL pop3::Reset()
{
char buf [512];
wsprintf (buf, "RSET \r\n");
m_PopServer.Send(buf, strlen (buf));
if (CheckResponse(RSET_CHECK)==FALSE)
return FALSE;
else
return TRUE;
}
// MsgContents will hold the msg body
BOOL pop3::Retrieve(int MsgNumber)
{
char buf [512];
ZeroMemory(buf, 512);
wsprintf (buf, "RETR %d\r\n",MsgNumber );
m_PopServer.Send(buf, strlen (buf));
if (CheckResponse(RETR_CHECK)==FALSE)
return FALSE;
else
return TRUE;
}
BOOL pop3::Statistics()
{
char buf [512];
wsprintf (buf, "STAT \r\n");
m_PopServer.Send(buf, strlen (buf));
if (CheckResponse(STAT_CHECK)==FALSE)
return FALSE;
else
return TRUE;
}
CString pop3::GetMsgContents()
{
return m_MsgContents;
}
int pop3::GetNumberOfMails()
{
return m_NumberMail;
}
int pop3::GetTotalMailSize()
{
return m_TotalSize;
}
BOOL pop3::Connect()
{
Connect(m_Host, m_User, m_Password);
return TRUE;
}
void pop3::SetHost(CString & Host)
{
m_Host = Host;
}
CString pop3::GetHost()
{
return m_Host;
}
void pop3::SetUser(CString & User)
{
m_User = User;
}
CString pop3::GetUser()
{
return m_User;
}
void pop3::SetPassword(CString & Password)
{
m_Password = Password;
}
CString pop3::GetPassword()
{
return m_Password;
}
BOOL pop3::CheckResponse(int ResponseType)
{
char buf [1000];
for (int i=0;i<512;i++)
buf[i]='\0';
m_PopServer.Receive(buf, sizeof(buf));
switch (ResponseType)
{
case CONNECTION_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Bad Connection");
return FALSE;
}
break;
case USER_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Bad User Name");
return FALSE;
}
break;
case PASSWORD_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Bad Password Name");
return FALSE;
}
break;
case QUIT_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Error occured during QUIT");
return FALSE;
}
break;
case DELETE_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Error occured during DELE");
return FALSE;
}
break;
case RSET_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Error occured during RSET");
return FALSE;
}
break;
case STAT_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Error occured during STAT");
return FALSE;
}
else
{
BOOL EmailNumber = TRUE;
for (char *p = buf; *p != '\0'; p++)
{
if (*p == '\t' || *p == ' ')
{
if(EmailNumber == TRUE)
{
m_NumberMail = atoi(p);
EmailNumber = FALSE;
}
else
{
m_TotalSize = atoi(p);
return TRUE;
}
}
}
}
break;
case NOOP_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Error occured during NOOP");
AfxMessageBox("NOOP ERR!");
return FALSE;
}
break;
case LIST_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Error occured during LIST");
return FALSE;
}
else
{
m_PopServer.Receive(buf, sizeof(buf));
for (char *p = buf; *p != '.'; p++)
if (*p == '\t' || *p == ' ')
m_SizeOfMsg.Add(atoi(p));
}
break;
case RETR_CHECK:
if (strnicmp(buf,"-ERR", 4) == 0)
{
m_ErrorMessage = _T("Error occured during RETR");
return FALSE;
}
else
{
char temp[9000];
char buf1 [512];
wsprintf (buf1, "NOOP \r\n");
m_MsgContents = "";
do
{
for(int k = 0; k <1000; k++)
for(int l = 0; l <10000; l++)
{}//sleep(1111);
int i = m_PopServer.Receive(temp, sizeof(temp));
m_MsgContents += temp;
int t = m_MsgContents.Find("\n.\r\n\0");
if(t>0)
m_MsgContents = m_MsgContents.Left(t);
t = m_MsgContents.Find("+OK ");
if(t>0)
{
m_MsgContents = m_MsgContents.Left(t);
break;
}
m_PopServer.Send(buf1, strlen (buf1));
if(i <= 30 && strnicmp(temp,"+OK ",4) == 0)
break;
}while(1);
}
break;
}
return TRUE;
}
CString pop3::GetErrorMessage()
{
return m_ErrorMessage;
}
BOOL pop3::List()
{
char buf [512];
wsprintf (buf, "LIST \r\n");
m_PopServer.Send(buf, strlen (buf));
if (CheckResponse(LIST_CHECK)==FALSE)
return FALSE;
else
return TRUE;
}
頭文件;
// pop3.h: interface for the pop3 class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_POP3_H__A7D9D85A_D946_48CE_AA1F_3C1A2D894BE7__INCLUDED_)
#define AFX_POP3_H__A7D9D85A_D946_48CE_AA1F_3C1A2D894BE7__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define CONNECTION_CHECK 0
#define USER_CHECK 1
#define PASSWORD_CHECK 2
#define QUIT_CHECK 3
#define DELETE_CHECK 4
#define RSET_CHECK 5
#define STAT_CHECK 6
#define NOOP_CHECK 7
#define LIST_CHECK 8
#define RETR_CHECK 9
class pop3
{
public:
pop3();
virtual ~pop3();
BOOL List();
CWordArray m_SizeOfMsg;
CString GetErrorMessage();
CString GetPassword();
void SetPassword(CString& Password);
CString GetUser();
void SetUser(CString& User);
CString GetHost();
void SetHost(CString& Host);
BOOL Connect();
int GetTotalMailSize();
int GetNumberOfMails();
CString GetMsgContents();
BOOL Statistics();
BOOL Retrieve(int MsgNumber);
BOOL Reset();
int GetMessageSize(int MsgNumber);
BOOL Noop();
BOOL Disconnect();
BOOL Delete(int& MsgNumber);
BOOL Connect(CString& Host, CString& User, CString& Password);
private:
CString m_ErrorMessage;
BOOL CheckResponse(int ResponseType);
CString m_Password;
CString m_User;
CString m_Host;
CString m_MsgContents;
int m_TotalSize;
int m_NumberMail;
CSocket m_PopServer;
};
#endif // !defined(AFX_POP3_H__A7D9D85A_D946_48CE_AA1F_3C1A2D894BE7__INCLUDED_)
連不上;想起來還沒用過foxmail;foxmail也是從桌面直接收郵件的;
foxmail也連不上;上網查了一下;原來自己郵箱的pop3和smtp功能默認是關閉的;
在郵箱設置裏面開啓;需要手機驗證;爲安全,如果不從桌面收郵件,不要開啓;
然後foxmail可以收郵件了 ;其設置如下;
再試程序;還是不能連接;
再用telnet,還是連不上;
修改代碼裏面連接端口;試一下端口爲110的郵箱;運行結果如下;
連不了;telnet此郵箱看一下;
客戶端禁止;郵箱的pop3和smtp功能默認關閉;又要手機驗證;先不搞了;