说明1:邮箱所使用的密码必须是第三方登录密码,而不是邮箱的初始密码,否则登录不了SMTP服务器
说明2:使用Telnet的方式登录,具体可以了解一下Telnet登录smtp服务器的方式
说明3:win10也有Telnet登录方式,只不过默认是关闭的,具体如何开启网上很多教程
SendMail.h
#ifndef CSENDMAIL_H
#define CSENDMAIL_H#include
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
using namespace std;#define SLEEPTIME 500class CSendMail
{
public:CSendMail();~CSendMail();bool SetSMTP(string address, int port);//登录bool LoginSMTP(string Email,string Password);//设置发送目标,邮箱,标题,内容,是否有附件bool SetTargetEmail(string Email, string title, string body, bool enclosure=false);设置附件路径bool SetEnclPath(vectorfilename);//发送bool Send();
protected:int GetError(int flags=0);bool SendEnclosure();
private:string m_UserEMail;string m_PassWord;string m_STMPAddress;int m_STMPPort;bool m_Login;vector m_Filename;/*---------------Socket------------*/WSADATA m_Wsadata;sockaddr_in m_STMPAddr;SOCKET m_SMTPSocket;
};#endif /* CSENDMAIL_H */
SendMail.cpp
#include "stdafx.h"
#include "SendMail.h"static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";CSendMail::CSendMail()
{//测试使用126,如有需要后面再更改m_STMPAddress = "smtp.126.com";//很多邮箱的默认端口都是25m_STMPPort = 25;m_Login = false;}
CSendMail::~CSendMail()
{}
//base64加密
char* base64Encode(char const* origSigned, unsigned origLength)
{unsigned char const* orig &#61; (unsigned char const*)origSigned; // in case any input bytes have the MSB set if (orig &#61;&#61; NULL) return NULL;unsigned const numOrig24BitValues &#61; origLength / 3;bool havePadding &#61; origLength > numOrig24BitValues * 3;bool havePadding2 &#61; origLength &#61;&#61; numOrig24BitValues * 3 &#43; 2;unsigned const numResultBytes &#61; 4 * (numOrig24BitValues &#43; havePadding);char* result &#61; new char[numResultBytes &#43; 3]; // allow for trailing &#39;/0&#39; // Map each full group of 3 input bytes into 4 output base-64 characters: unsigned i;for (i &#61; 0; i > 2) & 0x3F];result[4 * i &#43; 1] &#61; base64Char[(((orig[3 * i] & 0x3) <<4) | (orig[3 * i &#43; 1] >> 4)) & 0x3F];result[4 * i &#43; 2] &#61; base64Char[((orig[3 * i &#43; 1] <<2) | (orig[3 * i &#43; 2] >> 6)) & 0x3F];result[4 * i &#43; 3] &#61; base64Char[orig[3 * i &#43; 2] & 0x3F];}// Now, take padding into account. (Note: i &#61;&#61; numOrig24BitValues) if (havePadding){result[4 * i &#43; 0] &#61; base64Char[(orig[3 * i] >> 2) & 0x3F];if (havePadding2){result[4 * i &#43; 1] &#61; base64Char[(((orig[3 * i] & 0x3) <<4) | (orig[3 * i &#43; 1] >> 4)) & 0x3F];result[4 * i &#43; 2] &#61; base64Char[(orig[3 * i &#43; 1] <<2) & 0x3F];}else{result[4 * i &#43; 1] &#61; base64Char[((orig[3 * i] & 0x3) <<4) & 0x3F];result[4 * i &#43; 2] &#61; &#39;&#61;&#39;;}result[4 * i &#43; 3] &#61; &#39;&#61;&#39;;}result[numResultBytes] &#61; &#39;\0&#39;;return result;
}
bool CSendMail::SetSMTP(string address, int port)
{return true;
}
int CSendMail::GetError(int flags)
{Sleep(SLEEPTIME);char buff[10240];//有时候错误返回比较长&#xff0c;多留点空间int len &#61; recv(m_SMTPSocket, buff, 10240, 0);if (len <0)return len;buff[len] &#61; 0;string data &#61; buff;printf("%s\n", buff);int iRe &#61; atoi(data.substr(0, 3).c_str());return iRe;
}
bool CSendMail::LoginSMTP(string Email, string Password)
{if (Email.empty() || Password.empty())return false;m_UserEMail &#61; Email;m_PassWord &#61; Password;if (WSAStartup(MAKEWORD(2, 0), &m_Wsadata) !&#61; 0){//cout <<"StartUp WSA Failed" <h_addr_list[0], hptr->h_length);//printf("IP of smpt.126.com is : %d:%d:%d:%d", m_STMPAddr.sin_addr.S_un.S_un_b.s_b1, m_STMPAddr.sin_addr.S_un.S_un_b.s_b2, m_STMPAddr.sin_addr.S_un.S_un_b.s_b3, m_STMPAddr.sin_addr.S_un.S_un_b.s_b4);//链接if (connect(m_SMTPSocket, (sockaddr*)&m_STMPAddr, sizeof(m_STMPAddr))&#61;&#61;SOCKET_ERROR ){//cout <<"connect failed!" <}
bool CSendMail::SetEnclPath(vectorfilename)
{m_Filename.clear();for (int i &#61; 0; i }
bool CSendMail::SetTargetEmail(string Email, string title, string body, bool enclosure)
{if (!m_Login)return false;string bufferMailFrom&#61; "mail from:<" &#43; m_UserEMail &#43; ">\r\n";send(m_SMTPSocket, bufferMailFrom.c_str(), strlen(bufferMailFrom.c_str()), 0);if (GetError() !&#61; 250){cout <<"SetTargetEmail failed!" <\r\n";send(m_SMTPSocket, bufferRcptTo.c_str(), strlen(bufferRcptTo.c_str()), 0);if (GetError() !&#61; 250){cout <<"SetTargetEmail failed!" <}
bool CSendMail::SendEnclosure()
{if (!m_Login)return false;for (int i &#61; 0; i}
bool CSendMail::Send()
{if (!m_Login)return false;char bufferPoint[] &#61; "--&#64;boundary&#64;--End--\r\n.\r\n";send(m_SMTPSocket, bufferPoint, strlen(bufferPoint), 0);//if (GetError(1) !&#61; 250)//{// cout <<"Send failed!" <}