郵件內文及其附件的發送的C++實現

來源:互聯網
上載者:User

       這段代碼我花了整整一天來編寫,如果轉載,請註明出處,謝謝。

   前面的一篇文章已經講了如何發送郵件內文,原理我就不再敘述了,要瞭解的同學請到這裡查看!

   http://blog.csdn.net/lishuhuakai/article/details/27503503

   網上很多發送郵件附件的代碼都不能用,所以我用心寫了一個,直接封裝成了一個類,需要的同學可以直接調用這個類來發送郵件,純c++代碼。(在VS2013下測試完美通過!)

   廢話不多說,直接上代碼!

   Smtp.h

#ifndef __SMTP_H__ //避免重複包含#define __SMTP_H__#include <iostream>#include <list>#include <WinSock2.h>using namespace std;const int MAXLEN = 1024;const int MAX_FILE_LEN = 6000;static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";struct FILEINFO /*用來記錄檔案的一些資訊*/{char fileName[128]; /*檔案名稱*/char filePath[256]; /*檔案絕對路徑*/};class CSmtp{public:CSmtp(void);CSmtp(int port,string srvDomain,//smtp伺服器網域名稱string userName,//使用者名稱string password,//密碼string targetEmail, //目的郵件地址string emailTitle,  //主題string content       //內容);public:~CSmtp(void);public:int port;public:string domain;string user;string pass;string targetAddr;string title;string content;/*為了方便添加檔案,刪除檔案神馬的,使用list容器最為方便,相信大家在資料結構裡面都學過*/list <FILEINFO *> listFile;public:char buff[MAXLEN + 1];int buffLen;SOCKET sockClient;//用戶端的通訊端public:bool CreateConn(); /*建立串連*/bool Send(string &message);bool Recv();void FormatEmailHead(string &email);//格式化要發送的郵件標頭部int Login();bool SendEmailHead();//發送郵件標頭部資訊bool SendTextBody();    //發送文本資訊//bool SendAttachment();    //發送附件int SendAttachment_Ex();bool SendEnd();public:void AddAttachment(string &filePath); //添加附件void DeleteAttachment(string &filePath); //刪除附件void DeleteAllAttachment(); //刪除所有的附件void SetSrvDomain(string &domain);void SetUserName(string &user);void SetPass(string &pass);void SetTargetEmail(string &targetAddr);void SetEmailTitle(string &title);void SetContent(string &content);void SetPort(int port);int SendEmail_Ex();/*關於錯誤碼的說明:1.網路錯誤導致的錯誤2.使用者名稱錯誤3.密碼錯誤4.檔案不存在0.成功*/char* base64Encode(char const* origSigned, unsigned origLength);};#endif // !__SMTP_H__


      Smtp.cpp

#include "Smtp.h"#include <iostream>#include <fstream>using namespace std;#pragma  comment(lib, "ws2_32.lib")/*連結ws2_32.lib動態連結程式庫*//*base64採用別人的編碼,不過,這不是重點,重點是我完成了我的一個比較好的郵件發送用戶端*/char* CSmtp::base64Encode(char const* origSigned, unsigned origLength){unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB setif (orig == NULL) return NULL;unsigned const numOrig24BitValues = origLength / 3;bool havePadding = origLength > numOrig24BitValues * 3;bool havePadding2 = origLength == numOrig24BitValues * 3 + 2;unsigned const numResultBytes = 4 * (numOrig24BitValues + havePadding);char* result = new char[numResultBytes + 3]; // allow for trailing '/0'// Map each full group of 3 input bytes into 4 output base-64 characters:unsigned i;for (i = 0; i < numOrig24BitValues; ++i){result[4 * i + 0] = base64Char[(orig[3 * i] >> 2) & 0x3F];result[4 * i + 1] = base64Char[(((orig[3 * i] & 0x3) << 4) | (orig[3 * i + 1] >> 4)) & 0x3F];result[4 * i + 2] = base64Char[((orig[3 * i + 1] << 2) | (orig[3 * i + 2] >> 6)) & 0x3F];result[4 * i + 3] = base64Char[orig[3 * i + 2] & 0x3F];}// Now, take padding into account.  (Note: i == numOrig24BitValues)if (havePadding){result[4 * i + 0] = base64Char[(orig[3 * i] >> 2) & 0x3F];if (havePadding2){result[4 * i + 1] = base64Char[(((orig[3 * i] & 0x3) << 4) | (orig[3 * i + 1] >> 4)) & 0x3F];result[4 * i + 2] = base64Char[(orig[3 * i + 1] << 2) & 0x3F];}else{result[4 * i + 1] = base64Char[((orig[3 * i] & 0x3) << 4) & 0x3F];result[4 * i + 2] = '=';}result[4 * i + 3] = '=';}result[numResultBytes] = '\0';return result;}CSmtp::CSmtp(void){this->content = "";this->port = 25;this->user = "";this->pass = "";this->targetAddr = "";this->title = "";this->domain = "";WORD wVersionRequested;WSADATA wsaData;int err;wVersionRequested = MAKEWORD(2, 1);err = WSAStartup(wVersionRequested, &wsaData);this->sockClient = 0;}CSmtp::~CSmtp(void){DeleteAllAttachment();closesocket(sockClient);WSACleanup();}CSmtp::CSmtp(int port,string srvDomain,string userName,string password,string targetEmail,string emailTitle,string content){this->content = content;this->port = port;this->user = userName;this->pass = password;this->targetAddr = targetEmail;this->title = emailTitle;this->domain = srvDomain;WORD wVersionRequested;WSADATA wsaData;int err;wVersionRequested = MAKEWORD(2, 1);err = WSAStartup(wVersionRequested, &wsaData);this->sockClient = 0;}bool CSmtp::CreateConn(){//為建立socket對象做準備,初始化環境SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0); //建立socket對象SOCKADDR_IN addrSrv;HOSTENT* pHostent;pHostent = gethostbyname(domain.c_str());  //得到有關於網域名稱的資訊addrSrv.sin_addr.S_un.S_addr = *((DWORD *)pHostent->h_addr_list[0]);//得到smtp伺服器的網路位元組序的ip地址   addrSrv.sin_family = AF_INET;addrSrv.sin_port = htons(port);int err = connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));   //向伺服器發送請求 if (err != 0){return false;//printf("連結失敗\n");}this->sockClient = sockClient;if (false == Recv()){return false;}return true;}bool CSmtp::Send(string &message){int err = send(sockClient, message.c_str(), message.length(), 0);if (err == SOCKET_ERROR){return false;}string message01;cout << message.c_str() << endl;return true;}bool CSmtp::Recv(){memset(buff, 0, sizeof(char)* (MAXLEN + 1));int err = recv(sockClient, buff, MAXLEN, 0); //接收資料if (err == SOCKET_ERROR){return false;}buff[err] = '\0';cout << buff << endl;return true;}int CSmtp::Login(){string sendBuff;sendBuff = "EHLO ";sendBuff += user;sendBuff += "\r\n";if (false == Send(sendBuff) || false == Recv()) //既接收也發送{return 1; /*1表示發送失敗由於網路錯誤*/}sendBuff.empty();sendBuff = "AUTH LOGIN\r\n";if (false == Send(sendBuff) || false == Recv()) //請求登陸{return 1; /*1表示發送失敗由於網路錯誤*/}sendBuff.empty();int pos = user.find('@', 0);sendBuff = user.substr(0, pos); //得到使用者名稱char *ecode;/*在這裡順帶扯一句,關於string類的length函數與C語言中的strlen函數的區別,strlen計算出來的長度,只到'\0'字元為止,而string::length()函數實際上返回的是string類中字元數組的大小,你自己可以測試一下,這也是為什麼我下面不使用string::length()的原因*/ecode = base64Encode(sendBuff.c_str(), strlen(sendBuff.c_str()));sendBuff.empty();sendBuff = ecode;sendBuff += "\r\n";delete[]ecode;if (false == Send(sendBuff) || false == Recv()) //發送使用者名稱,並接收伺服器的返回{return 1; /*錯誤碼1表示發送失敗由於網路錯誤*/}sendBuff.empty();ecode = base64Encode(pass.c_str(), strlen(pass.c_str()));sendBuff = ecode;sendBuff += "\r\n";delete[]ecode;if (false == Send(sendBuff) || false == Recv()) //發送使用者密碼,並接收伺服器的返回{return 1; /*錯誤碼1表示發送失敗由於網路錯誤*/}if (NULL != strstr(buff, "550")){return 2;/*錯誤碼2表示使用者名稱錯誤*/}if (NULL != strstr(buff, "535")) /*535是認證失敗的返回*/{return 3; /*錯誤碼3表示密碼錯誤*/}return 0;}bool CSmtp::SendEmailHead()//發送郵件標頭部資訊{string sendBuff;sendBuff = "MAIL FROM: <" + user + ">\r\n";if (false == Send(sendBuff) || false == Recv()){return false; /*表示發送失敗由於網路錯誤*/}sendBuff.empty();sendBuff = "RCPT TO: <" + targetAddr + ">\r\n";if (false == Send(sendBuff) || false == Recv()){return false; /*表示發送失敗由於網路錯誤*/}sendBuff.empty();sendBuff = "DATA\r\n";if (false == Send(sendBuff) || false == Recv()){return false; //表示發送失敗由於網路錯誤}sendBuff.empty();FormatEmailHead(sendBuff);if (false == Send(sendBuff))//發送完頭部之後不必調用接收函數,因為你沒有\r\n.\r\n結尾,伺服器認為你沒有發完資料,所以不會返回什麼值{return false; /*表示發送失敗由於網路錯誤*/}return true;}void CSmtp::FormatEmailHead(string &email){/*格式化要發送的內容*/email = "From: ";email += user;email += "\r\n";email += "To: ";email += targetAddr;email += "\r\n";email += "Subject: ";email += title;email += "\r\n";email += "MIME-Version: 1.0";email += "\r\n";email += "Content-Type: multipart/mixed;boundary=qwertyuiop";email += "\r\n";email += "\r\n";}bool CSmtp::SendTextBody()  /*發送郵件文本*/{string sendBuff;sendBuff = "--qwertyuiop\r\n";sendBuff += "Content-Type: text/plain;";sendBuff += "charset=\"gb2312\"\r\n\r\n";sendBuff += content;sendBuff += "\r\n\r\n";return Send(sendBuff);}int CSmtp::SendAttachment_Ex() /*發送附件*/{for (list<FILEINFO *>::iterator pIter = listFile.begin(); pIter != listFile.end(); pIter++){cout << "Attachment is sending ~~~~~" << endl;cout << "Please be patient!" << endl;string sendBuff;sendBuff = "--qwertyuiop\r\n";sendBuff += "Content-Type: application/octet-stream;\r\n";sendBuff += " name=\"";sendBuff += (*pIter)->fileName;sendBuff += "\"";sendBuff += "\r\n";sendBuff += "Content-Transfer-Encoding: base64\r\n";sendBuff += "Content-Disposition: attachment;\r\n";sendBuff += " filename=\"";sendBuff += (*pIter)->fileName;sendBuff += "\"";sendBuff += "\r\n";sendBuff += "\r\n";Send(sendBuff);ifstream ifs((*pIter)->filePath, ios::in | ios::binary);if (false == ifs.is_open()){return 4; /*錯誤碼4表示檔案開啟錯誤*/}char fileBuff[MAX_FILE_LEN];char *chSendBuff;memset(fileBuff, 0, sizeof(fileBuff));/*檔案使用base64加密傳送*/while (ifs.read(fileBuff, MAX_FILE_LEN)){//cout << ifs.gcount() << endl;chSendBuff = base64Encode(fileBuff, MAX_FILE_LEN);chSendBuff[strlen(chSendBuff)] = '\r';chSendBuff[strlen(chSendBuff)] = '\n';send(sockClient, chSendBuff, strlen(chSendBuff), 0);delete[]chSendBuff;}//cout << ifs.gcount() << endl;chSendBuff = base64Encode(fileBuff, ifs.gcount());chSendBuff[strlen(chSendBuff)] = '\r';chSendBuff[strlen(chSendBuff)] = '\n';int err = send(sockClient, chSendBuff, strlen(chSendBuff), 0);if (err != strlen(chSendBuff)){cout << "檔案傳送出錯!" << endl;return 1;}delete[]chSendBuff;}return 0;}bool CSmtp::SendEnd() /*發送結尾資訊*/{string sendBuff;sendBuff = "--qwertyuiop--";sendBuff += "\r\n.\r\n";if (false == Send(sendBuff) || false == Recv()){return false;}cout << buff << endl;sendBuff.empty();sendBuff = "QUIT\r\n";return (Send(sendBuff) && Recv());}int CSmtp::SendEmail_Ex(){if (false == CreateConn()){return 1;}//Recv();int err = Login(); //先登入if (err != 0){return err; //錯誤碼必須要返回}if (false == SendEmailHead()) //發送EMAIL頭部資訊{return 1; /*錯誤碼1是由於網路的錯誤*/}if (false == SendTextBody()){return 1; /*錯誤碼1是由於網路的錯誤*/}err = SendAttachment_Ex();if (err != 0){return err;}if (false == SendEnd()){return 1; /*錯誤碼1是由於網路的錯誤*/}return 0; /*0表示沒有出錯*/}void CSmtp::AddAttachment(string &filePath) //添加附件{FILEINFO *pFile = new FILEINFO;strcpy_s(pFile->filePath, filePath.c_str());const char *p = filePath.c_str();strcpy_s(pFile->fileName, p + filePath.find_last_of("\\") + 1);listFile.push_back(pFile);}void CSmtp::DeleteAttachment(string &filePath) //刪除附件{list<FILEINFO *>::iterator pIter;for (pIter = listFile.begin(); pIter != listFile.end(); pIter++){if (strcmp((*pIter)->filePath, filePath.c_str()) == 0){FILEINFO *p = *pIter;listFile.remove(*pIter);delete p;break;}}}void CSmtp::DeleteAllAttachment() /*刪除所有的檔案*/{for (list<FILEINFO *>::iterator pIter = listFile.begin(); pIter != listFile.end();){FILEINFO *p = *pIter;pIter = listFile.erase(pIter);delete p;}}void CSmtp::SetSrvDomain(string &domain){this->domain = domain;}void CSmtp::SetUserName(string &user){this->user = user;}void CSmtp::SetPass(string &pass){this->pass = pass;}void CSmtp::SetTargetEmail(string &targetAddr){this->targetAddr = targetAddr;}void CSmtp::SetEmailTitle(string &title){this->title = title;}void CSmtp::SetContent(string &content){this->content = content;}void CSmtp::SetPort(int port){this->port = port;}

   測試代碼如下:

   main.cpp

#include "Smtp.h"#include <iostream>using namespace std;int main(){CSmtp smtp(25,/*smtp連接埠*/"smtp.163.com",/*smtp伺服器位址*/"it_is_just_a_test@163.com",/*你的郵箱地址*/"XXXXXXX",/*郵箱密碼*/"it_is_just_a_test@126.com",/*目的郵箱地址*/"好啊!",/*主題*/"XXX同學,你好。收到請回複。"/*郵件內文*/);/**//添加附件時注意,\一定要寫成\\,因為逸出字元的緣故string filePath("D:\\課程設計報告.doc");smtp.AddAttachment(filePath);*//*還可以調用CSmtp::DeleteAttachment函數刪除附件,還有一些函數,自己看標頭檔吧!*///filePath = "C:\\Users\\李懿虎\\Desktop\\sendEmail.cpp";//smtp.AddAttachment(filePath);int err;if ((err = smtp.SendEmail_Ex()) != 0){if (err == 1)cout << "錯誤1: 由於網路不暢通,發送失敗!" << endl;if (err == 2)cout << "錯誤2: 使用者名稱錯誤,請核對!" << endl;if (err == 3)cout << "錯誤3: 使用者密碼錯誤,請核對!" << endl;if (err == 4)cout << "錯誤4: 請檢查附件目錄是否正確,以及檔案是否存在!" << endl;}system("pause");return 0;}

        在VS2005下面,很有可能會出現帶中文的目錄或者中文名檔案打不開的情況,這個時候這麼解決:在兩個建構函式裡面的第一句加上:setlocale(LC_ALL,"Chinese-simplified");這一句話即可解決。

     在VS2013裡面貌似這個程式可以運行,微軟這朵奇葩。不說了。

     請盡量不使用QQ郵箱登陸,因為我試過,貌似連不上。qq郵箱的伺服器返回告訴你要求安全的串連,用SSL什麼的,哎,qq也是朵奇葩。

     附上我的一個工程:https://github.com/lishuhuakai/Mail

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.