SMTP协议和POP3协议就不详细阐述了 ,网上一搜索会有一大把给你解释的。
下面直接贴代码:
首先写一个class Sock类,这个类的功能主要是创建套接字(即int sock),用套接字來连接邮箱服务器。类里面还带有send_socket和recv_socket两个函数,其功能分别是向邮箱服务器发送协议指令和接收服务器反馈回来的信息。
sock.h文件
#ifndef __SOCK_H__
#define __SOCK_H__
#include <iostream>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <stdio.h>
#include <netinet/in.h>
class Sock
{
public:
Sock();
bool Connect(const char *host_id, constint &port);
void send_socket(const char *s);
int recv_socket();
constchar * get_recvbuf();
~Sock();
private:
char recvbuf[BUFSIZ];
int sock;
int num;
struct sockaddr_in server;
struct hostent *hp;
};
#endif
sock.cpp文件
#include"sock.h"
#include<stdexcept>
#include<netdb.h>
#include<string.h>
#include<sys/types.h>
Sock::Sock()
{
sock= socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1)
{
throw std::runtime_error("socketinit error\n");
}
}
Sock::~Sock()
{
close(sock);
}
bool Sock::Connect(constchar *host_id, const int &port)
{
server.sin_family = AF_INET;
hp = gethostbyname(host_id);