AppOctetStream.cpp
上传用户:weimei12
上传日期:2022-08-11
资源大小:185k
文件大小:4k
- // AppOctetStream.cpp: implementation of the CAppOctetStream class.
- // Author: Wes Clyburn (clyburnw@enmu.edu)
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "MIMEContentAgent.h"
- #include "AppOctetStream.h"
- #include "MIMECode.h"
- #include "Base64.h"
- #include "MailMessage.h"
- #include "MIMEMessage.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- // IMPORTANT: The number of bytes we read must be
- // a multiple of 3 because CBase64's Encode()
- // method will append padding characters ('=')
- // to make the output's size a multiple of 4.
- // (Base64 treats 3 8-bit bytes as 4 6-bit 'bytes').
- // MIME decoders are free to treat '=' as a signal
- // that there's no more data, so we don't want to pad
- // until we're supposed to.
- // When at the end of the file, the # of bytes read
- // may not be a multiple of 3, but that's okay
- // because we DO want the padding chars then.
- #define BYTES_TO_READ 54 // This number guarantess output won't
- // won't exceed line-length limit
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CAppOctetStream::CAppOctetStream(int nContentType)
- :CMIMEContentAgent(nContentType)
- {
- }
- CAppOctetStream::~CAppOctetStream()
- {
- }
- ///<summary>
- /// append it to the body
- ///</summary>
- BOOL CAppOctetStream::AppendPart(LPCTSTR szContent,
- LPCTSTR szParameters,
- int nEncoding,
- BOOL bPath,
- CString& strDestination)
- {
- ASSERT(szContent != NULL);
- if(NULL == szContent)
- {
- return FALSE;
- }
- CStdioFile fAttachment;
- // This class handles only file attachments, so it ignores the bPath parameter.
- if(!fAttachment.Open(szContent, (CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary)))
- {
- return FALSE;
- }
- strDestination += BuildSubHeader(szContent,
- szParameters,
- nEncoding,
- TRUE);
- AttachFile(&fAttachment, CMIMEMessage::BASE64, strDestination);
- fAttachment.Close();
- return TRUE;
- }
- ///<summary>
- /// Get the content type sting
- ///</summary>
- CString CAppOctetStream::GetContentTypeString()
- {
- CString s;
- s = _T("application/octet-stream");
- return s;
- }
- ///<summary>
- /// construct the sub-part header
- ///</summary>
- CString CAppOctetStream::BuildSubHeader(LPCTSTR szContent,
- LPCTSTR szParameters,
- int nEncoding,
- BOOL bPath)
- {
- CString strSubHeader;
- CString strTemp;
- TCHAR szFName[_MAX_FNAME] = {0};
- TCHAR szExt[_MAX_EXT] = {0};
- _tsplitpath_s(szContent, NULL, 0, NULL, 0, szFName, _MAX_FNAME, szExt, _MAX_EXT);
- // This class ignores szParameters and nEncoding.
- // It controls its own parameters and only handles Base64 encoding.
- if(bPath)
- {
- strTemp.Format(_T("; file=%s%s"), szFName, szExt);
- }
- else
- {
- strTemp = _T("");
- }
- strSubHeader.Format(_T("Content-Type: %s%srn"),
- GetContentTypeString(),
- strTemp);
- strSubHeader += _T("Content-Transfer-Encoding: base64rn");
- strTemp.Format(_T("Content-Disposition: attachment; filename=%s%srn"),
- szFName, szExt);
- strSubHeader += strTemp;
- // Signal end of sub-header.
- strSubHeader += _T("rn"); // Warning: numerous concatenations are inefficient.
- return strSubHeader;
- }
- ///<summary>
- /// attach the file to the mail
- ///</summary>
- void CAppOctetStream::AttachFile(CStdioFile* pFileAtt,
- int nEncoding,
- CString & strDestination)
- {
- ASSERT(pFileAtt != NULL);
- if(NULL == pFileAtt)
- {
- return;
- }
- CMIMECode* pEncoder = NULL;
- int nBytesRead = 0;
- TCHAR szBuffer[BYTES_TO_READ + 1] = {0};
- switch(nEncoding)
- {
- // This class handles only Base64 encoding, but others may be added here.
- default:
- case CMIMEMessage::BASE64:
- try
- {
- pEncoder = new CBase64;
- }
- catch(CMemoryException* e)
- {
- delete e;
- return;
- }
- }
- if(NULL == pEncoder)
- {
- return;
- }
- do
- {
- try
- {
- nBytesRead = pFileAtt->Read(szBuffer, BYTES_TO_READ);
- }
- catch(CFileException* e)
- {
- delete e;
- break;
- }
- szBuffer[nBytesRead] = 0;
- strDestination += pEncoder->Encode(szBuffer, nBytesRead);
- strDestination += _T("rn");
- } while(nBytesRead == BYTES_TO_READ);
- strDestination += _T("rn");
- delete pEncoder;
- }