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"(PGRpdiBzdHlsZT0ibGluZS1oZWlnaHQ6MS43O2NvbG9yOiMwMDA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hbiBzdHlsZT0iY29sb3I6IHJnYig0NCwg
NDQsIDQ0KTsgZm9udC1mYW1pbHk6IMvOzOUsICZxdW90O0FyaWFsIE5hcnJvdyZxdW90OywgYXJp
YWwsIHNlcmlmOyBsaW5lLWhlaWdodDogMjhweDsiPsb3yc/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

时间: 2024-10-23 23:36:35

POP邮件收取邮件 代码的相关文章

LumiSoft收取邮件(含邮件附件)

在.NET当中利用C#发送电子邮件很简单,微软也提供了默认的实现,但是收取电子邮件的操作却并没有提供解决方案.好在有一些第三方的解决方案可供选择,来简化程序员日常项目中的开发工作. 这里我选用LumiSoft,下载地址为http://www.lumisoft.ee/lswww/download/downloads/.当你打开链接,会发现里面有很多项目,为了实现收取邮件的功能,我们可以选择Examples里面的pop3_client_app.zip文件.我们解压缩,利用VS打开这个项目运行即可.这

node.js实现收取邮件,以及对特定邮件内容的自动回复,邮件正文保存的功能

废话不多说,直接上代码... var Imap = require('imap');var inspect = require('util').inspect;var MailParser = require("mailparser").MailParser;var nodemailer = require('nodemailer'); var fs = require('fs');var http = require('http'); var imap = new Imap({ us

C#邮件接收系统核心代码(接收POP3邮件IMAP邮件)

/* * Created by SharpDevelop. * User: Administrator * Date: 2013/11/18 * Time: 20:55 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Linq; using System.IO; using System.Collections.Gener

php收取邮件

有的网站用这种方式来激活用户.让你用注册邮箱给指定的邮箱发信:如土豆: /*让用户往公司的企业邮箱发信,收到后给他激活思路:用php+POP协议收取信件(php+socket编程)收到信件,分析发件人,并激活该用户让系统每个几分钟运行一次,用linux的crontab做定时任务*/ telnet 收取信件 ctrl +] 回车 输入user pass top 1 5 是第一封邮件的前5行的意思 如果要提取发件人信息,只取得1行信息就够了,top 1 1 <?php /**** 自学it网 高端P

python收取邮件(腾讯企业邮箱)

记录下我用python登入腾讯企业邮箱收取邮件 一.要用python收取邮件先要会3个类 imaplib  用来收取邮件 imaplib 里面我们注意几个方法: login  使用明文密码识别客户端.该密码将被引用. select 选择一个邮箱.返回的数据是邮箱中的邮件计数 (EXISTS响应).默认邮箱是'INBOX'.如果 设置了只读标志,则不允许对邮箱进行修改. search 搜索邮箱以查找匹配的邮件 BeautifulSoup 用来解析 text/html 和 txet/plain 类型

c#语言邮件发送参考代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Net.Mail; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using System.Co

你所不知道的邮件退信代码

退信代码说明:421HL:REP 该IP发送行为异常,存在接收者大量不存在情况,被临时禁止连接.请检查是否有用户发送病毒或者垃圾邮件,并核对发送列表有效性:421HL:ICC 该IP同时并发连接数过大,超过了网易的限制,被临时禁止连接.请检查是否有用户发送病毒或者垃圾邮件,并降低IP并发连接数量:421HL:IFC 该IP短期内发送了大量信件,超过了网易的限制,被临时禁止连接.请检查是否有用户发送病毒或者垃圾邮件,并降低发送频率:421HL:MEP 该IP发送行为异常,存在大量伪造发送域域名行为

python实现邮件发送完整代码(带附件发送方式)

实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import Headerfrom email.MIMEText import MIMETextfrom email.MIMEMultipart import MIMEMultipartimport smtplib, datetime def SendMailAttach(): msg = MIMEMultip

Smtp邮件发送系统公用代码整理—总结

1.前言 a.在软件开发中,我们经常能够遇到给用户或者客户推送邮件,推送邮件也分为很多方式,比如:推送一句话,推送一个网页等等.那么在系统开发中我们一般在什么情况下会使用邮件发送呢?下面我简单总结了一下:总结不全,纯属于整理封装的类. (1):用户注册(推送邮件,提示用户注册成功,牢记用户名密码) (2):修改密码(推送邮件,提示用户密码修改成功) (3):下订单(推送邮件,提示用户订单已下) (4):物流跟踪(推送邮件,跟踪物流信息) (5):广告推送(推送广告,提示用户近来公司近况或者新的商