用C++发邮件

  • d1_991156
    了解作者
  • 2.3KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-22 11:06
    上传日期
用C++发邮件,用起来超简单,稍微改下代码可以实现群发功能
邮件类.rar
  • 邮件类
  • CEmai.h
    1KB
  • CEmai.cpp
    4.6KB
内容介绍
// CEmai.cpp: implementation of the CCEmai class. // ////////////////////////////////////////////////////////////////////// #include "CEmai.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CCEmai::CCEmai() {//加载winsocket库 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup( ); return; } } CCEmai::~CCEmai() { //释放Socket库 WSACleanup(); } bool CCEmai::Init(const string &_smtpServer,const string &_username,const string & _password)//连接服务器并登陆 { smtpServer=_smtpServer; username=_username; pw=_password; hostent *ph = gethostbyname(smtpServer.c_str()); if( ph == NULL ) { cout<<"no host: "<<smtpServer<<endl; return false; } sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(25); //port of SMTP memcpy(&sin.sin_addr.S_un.S_addr, ph->h_addr_list[0], ph->h_length); //connect to the mail server s = socket(PF_INET, SOCK_STREAM, 0); if( connect(s, (sockaddr*)&sin, sizeof(sin)) ) { cerr<<"failed to connect the mail server"<<endl; return false; } // char recvBuffer[1024]; Recv(s, recvBuffer, sizeof(recvBuffer)); //wait for greeting message Send(s, "HELO " + smtpServer + "\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); //should recv "250 OK" //start to log in Send(s, (string)"auth login\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); //should recv "334 username:"(This is the decode message) Send(s, Base64Encode(username) + "\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); if( string(recvBuffer).substr(0, 3) != "334" ) { cout<<"username is error!"<<endl; return false; } Send(s, Base64Encode(pw) + "\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); if( string(recvBuffer).substr(0, 3) != "235") { cout<<"password error"<<endl; return false; } return true; // } void CCEmai::Send(SOCKET &s, string &data) { if( send(s, data.c_str(), data.length(), 0) == SOCKET_ERROR ) { cout<<"send data \""<<data<<"\" error"<<endl; } } void CCEmai::Recv(SOCKET &s, char *buf, int len) { memset(buf, 0, len); if( recv(s, buf, len, 0) == SOCKET_ERROR ) { cout<<"error, while receiving data"<<endl; } } string CCEmai::Base64Encode(const string &src) { int i, j, srcLen = src.length(); string dst(srcLen / 3 * 4 + 4, 0); for(i = 0, j= 0; i <=srcLen - 3; i+=3, j+=4) { dst[j] = (src[i] & 0xFC) >> 2; dst[j+1] = ((src[i] & 0x03) << 4) + ((src[i+1] & 0xF0) >> 4); dst[j+2] = ((src[i+1] & 0x0F) << 2) + ((src[i+2] & 0xC0) >> 6); dst[j+3] = src[i+2] & 0x3F; } if( srcLen % 3 == 1 ) { dst[j] = (src[i] & 0xFC) >> 2; dst[j+1] = ((src[i] & 0x03) << 4); dst[j+2] = 64; dst[j+3] = 64; j += 4; } else if( srcLen % 3 == 2 ) { dst[j] = (src[i] & 0xFC) >> 2; dst[j+1] = ((src[i] & 0x03) << 4) + ((src[i+1] & 0xF0) >> 4); dst[j+2] = ((src[i+1] & 0x0F) << 2); dst[j+3] = 64; j+=4; } static unsigned char *base64 = (unsigned char*)("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="); for(i = 0; i < j; ++i) { //map 6 bit value to base64 ASCII character dst[i] = base64[(int)dst[i]]; } return dst; } bool CCEmai::SendMessage(const string &to, const string &sub, const string &data) { char recvBuffer[1024]; //Set sender Send(s, "mail from:<" + username + ">\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); //should recv "250 Mail OK" //set receiver Send(s, "rcpt to:<" + to + ">\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); //should recv "250 Mail OK" //send data Send(s, (string)"data\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); //should recv "354 End data with <CR><LF>.<CR><LF>" Send(s, "to:" + to + "\r\n" + "subject:"+sub+"\r\n\r\n" + data + "\r\n.\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); return true; } bool CCEmai::Quit() { char recvBuffer[1024]; Send(s, (string)"quit\r\n"); Recv(s, recvBuffer, sizeof(recvBuffer)); closesocket(s); return true; }
评论
    相关推荐
    • C++ Primer
      C++经典教程,其内容是C++大师Stanley B. Lippman丰富的实践经验和C++标准委员会原负责人Josée Lajoie对C++标准深入理解的完美结合,已经帮助全球无数程序员学会了C++。 对C++基本概念和技术全面而且权威的阐述,对...
    • c++课件
      c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件
    • C++ PRrimer
      本书是久负盛名的C++经典教程,其内容是C++大师Stanley B. Lippman丰富的实践经验和C++标准委员会原负责人Josée Lajoie对C++标准深入理解的完美结合,已经帮助全球无数程序员学会了C++。本版对前一版进行了彻底的...
    • C++
      C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++
    • C++ primer
      本文档具有C++ primer 以及 C++ primer 标准答案各一份,内容清晰充实!希望与热爱C++的学友们一起同舟共济,努力学习!
    • C++ primer
      本书是久负盛名的C++经典教程,其内容是C++大师Stanley B. Lippman丰富的实践经验和C++标准委员会原负责人Josée Lajoie对C++标准深入理解的完美结合,已经帮助全球无数程序员学会了C++。本版对前一版进行了彻底的...
    • c++yuyanbiancheng
      这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!
    • effective C++
      有关C++编程方面的检验性介绍,对由C转向C++,和有C++编程基础的程序员有帮助,不过是英文版
    • C++ Primer
      这本处适合各个阶段的C++程序员,这本书可以帮助初学者快速入门,里面有最实用,最容易理解的代码;同时这也是有经验的C++程序员最好的一本参考手册
    • C++ Primer
      本书是久负盛名的C++经典教程引,其内容是C++大师Stanley B. Lippman丰富的实践经验和C++标准委员会原负责人Josée Lajoie对C++标准深入理解的完美结合,已经帮助全球无数程序员学会了C++