POP邮件收取邮件 代码
// 111111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <WinSock.h>
#include <iostream>
#include <string>
#pragma comment(lib,"WSock32.Lib") class ZBase64
{
public:
std::string Encode(const unsigned char* Data, int DataByte);
std::string Decode(const char* Data, int DataByte, int& OutByte);
}; std::string ZBase64::Encode(const unsigned char* Data, int DataByte)
{
//编码表
const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//返回值
std::string strEncode;
unsigned char Tmp[4] = { 0 };
int LineLength = 0;
for (int i = 0; i<(int)(DataByte / 3); i++)
{
Tmp[1] = *Data++;
Tmp[2] = *Data++;
Tmp[3] = *Data++;
strEncode += EncodeTable[Tmp[1] >> 2];
strEncode += EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
strEncode += EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
strEncode += EncodeTable[Tmp[3] & 0x3F];
if (LineLength += 4, LineLength == 76) { strEncode += "\r\n"; LineLength = 0; }
}
//对剩余数据进行编码
int Mod = DataByte % 3;
if (Mod == 1)
{
Tmp[1] = *Data++;
strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];
strEncode += EncodeTable[((Tmp[1] & 0x03) << 4)];
strEncode += "==";
}
else if (Mod == 2)
{
Tmp[1] = *Data++;
Tmp[2] = *Data++;
strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];
strEncode += EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
strEncode += EncodeTable[((Tmp[2] & 0x0F) << 2)];
strEncode += "=";
} return strEncode;
} std::string ZBase64::Decode(const char* Data, int DataByte, int& OutByte)
{
//解码表
const char DecodeTable[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62, // '+'
0, 0, 0,
63, // '/'
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
0, 0, 0, 0, 0, 0,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
};
//返回值
std::string strDecode;
int nValue;
int i = 0;
while (i < DataByte)
{
if (*Data != '\r' && *Data != '\n')
{
nValue = DecodeTable[*Data++] << 18;
nValue += DecodeTable[*Data++] << 12;
strDecode += (nValue & 0x00FF0000) >> 16;
OutByte++;
if (*Data != '=')
{
nValue += DecodeTable[*Data++] << 6;
strDecode += (nValue & 0x0000FF00) >> 8;
OutByte++;
if (*Data != '=')
{
nValue += DecodeTable[*Data++];
strDecode += nValue & 0x000000FF;
OutByte++;
}
}
i += 4;
}
else// 回车换行,跳过
{
Data++;
i++;
}
}
return strDecode;
}
//===============================
class SocketInit {
public:
SocketInit() {
WSADATA wsaData;
int err;
err = WSAStartup(MAKEWORD(2,2), &wsaData);
if (err) {
std::cerr << "socket init error " << err << std::endl;
exit(err);
}
}
~SocketInit() {
WSACleanup();
}
}; SocketInit gSockInit;
const int BUF_MAX_LEN = 1024*10; const char* strUser = ""; //邮箱用户名
const char* strPassWord = ""; //邮箱密码 int main()
{
// base64 解码示例
std::string mailStr(R"(PGRpdiBzdHlsZT0ibGluZS1oZWlnaHQ6MS43O2NvbG9yOiMwMDAwMDA7Zm9udC1zaXplOjE0cHg7
Zm9udC1mYW1pbHk6QXJpYWwiPjxzcGFuIHN0eWxlPSJjb2xvcjogcmdiKDQ0LCA0NCwgNDQpOyBm
b250LWZhbWlseTogy87M5SwgJnF1b3Q7QXJpYWwgTmFyc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Psb3yc/JvrP9oaPI57n7xOPP69LGs/3V4tCp
yb6z/bHqvMejrNPDUlNFVMP8we6ho9XiuPbD/MHusqKyu9XrttTEs9K7uPbM2LaotcTTyrz+o6y2
+MrH0saz/cv509DJvrP9seq8x6Osy/nS1MTjsrvQ6NKqzOG5qdPKvP7L99L9usW4+NXiuPbD/MHu
oaMmbmJzcDs8L3NwYW4+PC9kaXY+PGJyPjxicj48c3BhbiB0aXRsZT0ibmV0ZWFzZWZvb3RlciI+
PGRpdiBpZD0ibmV0ZWFzZV9tYWlsX2Zvb3RlciI+PGRpdiBzdHlsZT0iYm9yZGVyLXRvcDojQ0ND
IDFweCBzb2xpZDtwYWRkaW5nOjEwcHggNXB4O2ZvbnQtc2l6ZToxNXB4O2NvbG9yOiM3Nzc7bGlu
ZS1oZWlnaHQ6MjJweCI+08rP5Ljvw/zAtMHLo6Gjoc340tcxNjOhojEyNtPKz+S/ydLUt6LLzTNH
0tTJz7OstPPOxLz+o6E8L2Rpdj48L2Rpdj48L3NwYW4+)");
ZBase64 t;
int i;
std::string sOut = t.Decode(mailStr.c_str(), mailStr.size(), i);
std::cout << sOut << std::endl;
//==========================================
SOCKET mySock = socket(AF_INET, SOCK_STREAM, 0);//创建了可识别套接字 struct hostent *remoteHost;
struct in_addr addr; if ((remoteHost = gethostbyname("pop.126.com")) == NULL)
exit(1); if (remoteHost->h_addrtype == AF_INET)
{
addr.s_addr = *(u_long *)remoteHost->h_addr_list[0];
printf("IP Address #%d: %s\n", 0, inet_ntoa(addr));
} SOCKADDR_IN popServer;
popServer.sin_addr = addr;
popServer.sin_family = AF_INET;
popServer.sin_port = htons(110);
// step 1 connect
int ret = connect(mySock, (SOCKADDR*)&popServer, sizeof(SOCKADDR));
if (ret)
exit(ret); char receiveBuf[BUF_MAX_LEN] = {0};
ret = recv(mySock, receiveBuf, BUF_MAX_LEN,0);
if (SOCKET_ERROR == ret)
exit(ret);
std::cout << receiveBuf ; // step 2 sendUser
std::string SendMessage("USER ");
SendMessage += strUser;
SendMessage += "\r\n"; ret = send(mySock, SendMessage.c_str(), SendMessage.size(),0);
if (SOCKET_ERROR == ret)
exit(ret);
ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0);
if (SOCKET_ERROR == ret)
exit(ret);
std::cout << receiveBuf; //stecp 3 sendPass
SendMessage.clear();
SendMessage +="PASS ";
SendMessage += strPassWord;
SendMessage += "\r\n";
ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0);
if (SOCKET_ERROR == ret)
exit(ret);
ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0);
if (SOCKET_ERROR == ret)
exit(ret);
std::cout << receiveBuf; //stecp 4 test LIST
SendMessage.clear();
SendMessage += "LIST\r\n";
ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0);
if (SOCKET_ERROR == ret)
exit(ret);
ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0);
if (SOCKET_ERROR == ret)
exit(ret);
//std::cout << receiveBuf; std::cout << std::endl << std::endl << std::endl
<< std::endl << std::endl << std::endl
<< std::endl << std::endl << std::endl; /*
//stecp 5 test DELE
SendMessage.clear();
SendMessage += "DELE 2\r\n";
ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0);
if (SOCKET_ERROR == ret)
exit(ret);
//ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0);
if (SOCKET_ERROR == ret)
exit(ret);
std::cout << receiveBuf;
//ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0);
//if (SOCKET_ERROR == ret)
//exit(ret);
//std::cout << receiveBuf;
*/ //stecp 6 test RETR
SendMessage.clear();
SendMessage += "RETR 1\r\n";
ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0);
if (SOCKET_ERROR == ret)
exit(ret);
ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0);
if (SOCKET_ERROR == ret)
exit(ret);
std::cout << receiveBuf; std::cout << std::endl; ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0);
if (SOCKET_ERROR == ret)
exit(ret);
std::cout << receiveBuf; std::cout << std::endl; closesocket(mySock);
return 0;
}
参考此博客
http://yerik.blog.51cto.com/1662422/743998
POP邮件收取邮件 代码的更多相关文章
- php iamp 接收邮件,收取邮件,获取邮件列表
每次想写的时候吧,提笔忘字.等到再次使用,又得想半天,,,,,好尴尬. 这次一边做一边写. 心得,程序员从菜鸟往老鸟转变的重要一步,学英语,看文档,在此我万分感谢鸟哥,,,,没他php哪有官方的中文注 ...
- Python中使用SMTP发送邮件以及POP收取邮件
假设我们自己的电子邮件地址是from@163.com,对方的电子邮件地址是to@sina.com(这里的地址虚拟的),现在我们用Outlook或者Foxmail之类的软件写好邮件,填上对方的Email ...
- LumiSoft收取邮件(含邮件附件)
在.NET当中利用C#发送电子邮件很简单,微软也提供了默认的实现,但是收取电子邮件的操作却并没有提供解决方案.好在有一些第三方的解决方案可供选择,来简化程序员日常项目中的开发工作. 这里我选用Lumi ...
- Python3+smtplib+poplib+imaplib实现发送和收取邮件(以qq邮箱为例)
一.说明 1.1 程序说明 (1)smtp是邮件发送协议:pop和imap都是邮件接收协议,两者的区别通常的说法是imap的操作会同步到邮箱服务器而pop不会,表现上我也不是很清楚 (2)本程序实现使 ...
- Python学习笔记(四十八)POP3收取邮件
收取邮件就是编写一个MUA作为客户端,从MDA把邮件获取到用户的电脑或者手机上.收取邮件最常用的协议是POP协议,目前版本号是3,俗称POP3. Python内置一个poplib模块,实现了POP3协 ...
- 五十七 POP3收取邮件
SMTP用于发送邮件,如果要收取邮件呢? 收取邮件就是编写一个MUA作为客户端,从MDA把邮件获取到用户的电脑或者手机上.收取邮件最常用的协议是POP协议,目前版本号是3,俗称POP3. Python ...
- Python利用zmail收取邮件
收取邮件一般用pop和imap,这里使用国人大神开发的zmail来收取: 1 ''' 2 #利用zmail收取邮件 3 #只要几行代码 4 #安装库:pip3 install zmail 5 #国内大 ...
- C#邮件接收系统核心代码(接收POP3邮件IMAP邮件)
/* * Created by SharpDevelop. * User: Administrator * Date: 2013/11/18 * Time: 20:55 * * To change t ...
- 一步一步从原理跟我学邮件收取及发送 12.telnet命令行收一封信pop3
本系列上一篇文章中我们就说到了,这一次我们要说 pop3 收信了.虽然我觉得应该先说完 mime 格式,不过估计大家已经不耐烦了 -- 怎么老在说发送啊?我们要看收取! 好吧,来啦,来啦!收取邮 ...
随机推荐
- 1013:Digital Roots
注意:大数要用字符串表示! sprintf:字符串格式化命令 主要功能:将格式化的数据写入某个字符串缓冲区 头文件:<stdio.h> 原型 int sprintf( char *buff ...
- Oracle简单的SQL处理
--单表插入insert into hr.job(job_id,job_title,min_salary) values('IT','Project Manager',50000); insert i ...
- JDBC的操作步骤
JDBC的操作步骤 一.什么是JDBC JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问 ...
- nginx-vod-module && docker && docker-compose 测试
nginx-vod-module appdemo i reference [nginx-vod-module-docker](https://github.com/NYTimes/nginx-vod- ...
- pthread中取消线程
取消线程:告诉一个线程关掉自己,取消操作允许线程请求终止其所在进程中的任何其他线程.不希望或不需要对一组相关的线程执行进一步操作时,可以选择执行取消操作.取消线程的一个示例是异步生成取消条件. 对于c ...
- eclipse启动报错:An error has occurred.See the log file D:\eclipse\configuration\1552616709202.log
如题,Eclipse崩了,只能按它留下的线索去看了1552616709202.log: !SESSION -- ::08.739 ----------------------------------- ...
- RK3288 device descriptor read/64, error -32
CPU:RK3288 系统:Android 5.1 主板有两个USB接口,一个接USB摄像头,一个接身份证模块. 插入摄像头可以正常打开,再插入身份证模块时,摄像头就会卡主,而且身份证模块无法识别,内 ...
- Mac 下使用brew install 报错: Cowardly refusing to `sudo brew install'
Mac 下使用brew install 报错: localhost:infer-osx-v0.6.0 admin$ sudo brew install opam Error: Cowardly ref ...
- OneProxy常用参数说明
5.2.OneProxy常用参数说明 OneProxy的所有可用参数可通过oneproxy --help-all查看.所有参数均可以写入文件中,由OneProxy启动时加载 5.2.1.基本参数 -- ...
- 黄聪:中国大陆的所有IP段,中国电信所有IP段、中国铁通所有IP段、中国网通所有IP段。
中国大陆的所有IP段,中国电信所有IP段.中国铁通所有IP段.中国网通所有IP段. 中国大陆的所有IP段: 47.153.128.0 47.154.255.25558.14.0.0 58.25.255 ...