📄 sendemail.cpp
字号:
if (m_X_Mailer.size()) { if(!Send(m_X_Mailer.c_str(), m_X_Mailer.size())) throw("Error: Failed to send X-Mailer"); } if (m_X_Phone.size()) { if(!Send(m_X_Phone.c_str(), m_X_Phone.size())) throw("Error: Failed to send X-Phone"); } if(MessageID.size()) { StrStm.str(""); StrStm << "Message-ID: " << MessageID << CRLF_SET; if(!SendStr(StrStm)) throw("Error: Failed to send Message-ID"); } StrStm.str(""); StrStm << "To: "; for (std::list<std::string>::const_iterator email_addr_item = To.begin(); email_addr_item != To.end();email_addr_item++) { StrStm << email_addr_item->c_str() << m_EmailAddrSeperator; } StrStm << CRLF_SET; if(!SendStr(StrStm)) throw("Error: Failed to send TO_List"); StrStm.str(""); StrStm << "From: " << EmailAddrFormat(SendersEmailAddr) << m_Sender << CRLF_SET; if(!SendStr(StrStm)) throw("Error: Failed to send From"); if(m_ReplyToAddr.size()) { StrStm.str(""); StrStm << "Reply-To: " << m_ReplyToAddr.c_str() << CRLF_SET; if(!SendStr(StrStm)) throw("Error: Failed to send Reply-To"); } if(CC_List.size()) { strcpy(m_EmailBuffer,"CC: "); for (std::list<std::string>::const_iterator email_addr_item = CC_List.begin(); email_addr_item != CC_List.end();email_addr_item++) { strcat(m_EmailBuffer, email_addr_item->c_str()); strcat(m_EmailBuffer,m_EmailAddrSeperator.c_str()); } strcat(m_EmailBuffer, CRLF_SET.c_str()); if(!SendStr(m_EmailBuffer)) throw("Error: Failed to send CC_List"); } StrStm.str(""); StrStm << "Subject: " << Subject << CRLF_SET; if(!SendStr(StrStm)) throw("Error: Failed to send Subject"); if(AttachmentPath_List) { StrStm.str(""); StrStm << "MIME-Version: 1.0" << CRLF_SET; StrStm << "Content-type: multipart/mixed; boundary=\"#BOUNDARY#\"" << CRLF_SET; SendStr(StrStm); } strcpy(m_EmailBuffer, CRLF_SET.c_str()); if(!SendStr(m_EmailBuffer)) throw("Error: Failed to send empty line needed after headers(1)"); strcpy(m_EmailBuffer, CRLF_SET.c_str()); if(!SendStr(m_EmailBuffer)) throw("Error: Failed to send empty line needed after headers(2)"); if(AttachmentPath_List) { StrStm.str(""); StrStm << CRLF_SET; StrStm << "--#BOUNDARY#" << CRLF_SET; StrStm << "Content-Type: text/plain; charset=ISO-8859-1" << CRLF_SET; StrStm << "Content-Transfer-Encoding: quoted-printable" << CRLF_SET; StrStm << CRLF_SET; SendStr(StrStm); } if(!Send(Body.c_str(), Body.size())) throw("Error: Failed to send send message text"); if(AttachmentPath_List) { for (std::list<std::string>::const_iterator attachment_file_item = AttachmentPath_List->begin(); attachment_file_item != AttachmentPath_List->end(); attachment_file_item++) { if (!Encode64File(*attachment_file_item)) { throw("Error: Failed to attachment_file_item"); } } strcpy(m_EmailBuffer, CRLF_SET.c_str()); strcat(m_EmailBuffer, "--#BOUNDARY#--"); SendStr(m_EmailBuffer); } if(!Send("\r\n.\r\n", 5)) throw("Error: Failed to send message terminator"); if(!VerifyReply(CHK_250_REPLY )) throw("Error: Failed to Recv 250 reply after sending message terminator"); SendResetIf_Throw=false; //No use sending reset if fail at this point strcpy(m_EmailBuffer, "QUIT" ); strcat(m_EmailBuffer, CRLF_SET.c_str()); if(!SendStr(m_EmailBuffer)) throw("Error: Failed to send QUIT message"); if(!VerifyReply(CHK_221_REPLY )) throw("Error: Failed to Recv 221 reply after sending QUIT message"); } catch(const char* ErrMsg) { ReturnValue = ErrMsg; if (ReturnValueFromReceive.size()) { ReturnValue += " Last Rcvd = " + ReturnValueFromReceive; } if (SendResetIf_Throw) { strcpy(m_EmailBuffer, "RESET"); strcat(m_EmailBuffer, CRLF_SET.c_str()); SendStr(m_EmailBuffer); strcpy(m_EmailBuffer, "QUIT"); strcat(m_EmailBuffer, CRLF_SET.c_str()); SendStr(m_EmailBuffer); } } shutdown (m_socket, 2); m_socket = 0; return ReturnValue;}bool SendEmail::SendStr(const StrngStream &Src){ return SendStr(Src.str().c_str());}bool SendEmail::SendStr(const char* Buffer){ return Send(Buffer, strlen(Buffer));}bool SendEmail::VerifyReply(const char* szReplyString){ return Receive(m_EmailBuffer, MAX_LINE_SIZE, 0, szReplyString);}bool SendEmail::Receive(char* szBuffer, int nLenMax, int nFlags, const char* szReplyString){ int nRes = recv(m_socket, szBuffer, nLenMax, nFlags); ReturnValueFromReceive = szBuffer; if (nRes == -1 ) return false; *(szBuffer + nRes ) = '\0'; char* szPtr = strtok( szBuffer, "\n" ); while(szPtr) { if(*(szPtr + 3) == ' ') { if(!strncmp(szPtr, szReplyString, strlen(szReplyString))) return true; else return false; } else szPtr = strtok( 0, "\n" ); } return false;}bool SendEmail::Send(const char *szBuffer, size_t nLen){ size_t nCnt = 0; while( nCnt < nLen ) { int nRes = send( m_socket, szBuffer + nCnt, nLen - nCnt, 0); if( nRes == -1) return false; else nCnt += nRes; } return true;}bool SendEmail::Encode64File(const std::string &FileName) { std::ifstream file(FileName.c_str(), std::ios::in|std::ios::binary); if (file.fail()) { return false; } file.unsetf(std::ios::skipws); std::vector<unsigned char> vBytes; for(unsigned char uc = 0;;) { file >> uc; if (file.eof()) break; vBytes.push_back(uc); } std::string FileNameWithOutPath; if ( (FileName.find_last_of('/')==std::string::npos)) { FileNameWithOutPath = FileName ; } else { FileNameWithOutPath = FileName.c_str()+FileName.find_last_of('/')+1; } if ((FileNameWithOutPath.find_last_of('\\')==std::string::npos)) { FileNameWithOutPath = FileNameWithOutPath; } else { FileNameWithOutPath = FileNameWithOutPath.c_str()+FileNameWithOutPath.find_last_of('\\')+1; } std::string encodedBuffer = CRLF_SET + CRLF_SET + "--#BOUNDARY#"; encodedBuffer += CRLF_SET + "Content-Type: text/plain; charset=ISO-8859-1; name=" + FileNameWithOutPath; encodedBuffer += CRLF_SET + "Content-Transfer-Encoding: base64"; encodedBuffer += CRLF_SET + "Content-Disposition: attachment; filename=" + FileNameWithOutPath; encodedBuffer += CRLF_SET + CRLF_SET; Send(encodedBuffer.c_str(), encodedBuffer.size()); Encode64(vBytes, &encodedBuffer, true); Send(encodedBuffer.c_str(), encodedBuffer.size()); return true;}std::string SendEmail::Encode64String(const std::string &Src){ return Encode64(std::vector<unsigned char>(Src.begin(), Src.end()),0 ,false);}inline char SendEmail::Encode64Byte(unsigned char uc){ if (uc < 26) return 'A'+uc; if (uc < 52) return 'a'+(uc-26); if (uc < 62) return '0'+(uc-52); if (uc == 62) return '+'; return '/';}inline const char* SendEmail::Encode64(const std::vector<unsigned char> & vBytes, std::string *Dest, bool EndWithCR){ static std::string DefaultStr; std::string &retval = (Dest)?(*Dest):DefaultStr; retval = ""; if (vBytes.size() == 0) return retval.c_str(); for (int i=0;i<vBytes.size();i+=3) { unsigned char by1=0,by2=0,by3=0; by1 = vBytes[i]; if (i+1<vBytes.size()) { by2 = vBytes[i+1]; } if (i+2<vBytes.size()) { by3 = vBytes[i+2]; } unsigned char by4=0,by5=0,by6=0,by7=0; by4 = by1>>2; by5 = ((by1&0x3)<<4)|(by2>>4); by6 = ((by2&0xf)<<2)|(by3>>6); by7 = by3&0x3f; retval += Encode64Byte(by4); retval += Encode64Byte(by5); if (i+1<vBytes.size()) { retval += Encode64Byte(by6); } else { retval += "="; } if (i+2<vBytes.size()) { retval += Encode64Byte(by7); } else { retval += "="; } if (i % (76/4*3) == 0) { if (EndWithCR) retval += "\r\n"; else retval += "\0"; } } return retval.c_str();}bool SendEmailSimple(const char* HostName, const char* UserID, const char* To, const char* Subject, const char* Body, const char* UserPassword /* = NULL */, const char* FileName_Attach /* = NULL */){ std::list<std::string> Attachments; if (FileName_Attach) Attachments.push_back(FileName_Attach); std::string Results = SendEmail(HostName, UserID, (UserPassword)?UserPassword:"")(To, Subject, Body, NULL, NULL, (FileName_Attach)?&Attachments:NULL); if (Results != SendEmail::NO_ERRORS) return false; return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -