⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appoctetstream.cpp

📁 EMail VC++程序开发
💻 CPP
字号:
// AppOctetStream.cpp: implementation of the CAppOctetStream class.
// 

#include "stdafx.h"
#include "AppOctetStream.h"
#include "Base64.h"
#include "MIMEMessage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define BYTES_TO_READ 54 

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAppOctetStream::CAppOctetStream(int nContentType)
	:CMIMEContentAgent( nContentType )
{
}

CAppOctetStream::~CAppOctetStream()
{
}

BOOL CAppOctetStream::AppendPart(LPCTSTR szContent, 
								 LPCTSTR szParameters, 
								 int nEncoding, 
								 BOOL bPath, 
								 CString & sDestination)
{
	CStdioFile fAttachment;

	ASSERT(szContent);
	if (szContent == NULL)
		return FALSE;
	if (!fAttachment.Open(szContent,(CFile::modeRead | CFile::shareDenyWrite
		| CFile::typeBinary)))
		return FALSE;
	sDestination += build_sub_header(szContent,szParameters,nEncoding,TRUE);
	attach_file( &fAttachment,CMIMEMessage::BASE64,sDestination);
	fAttachment.Close();
	return TRUE;
}


CString CAppOctetStream::build_sub_header(LPCTSTR szContent,LPCTSTR szParameters,
										  int nEncoding,BOOL bPath)
{
	CString sSubHeader;
	CString sTemp;
	TCHAR szFName[ _MAX_FNAME ];
	TCHAR szExt[ _MAX_EXT ];

	_tsplitpath( szContent, NULL, NULL, szFName, szExt );

	if (bPath)
		sTemp.Format("; file=%s%s",szFName,szExt);
	else
		sTemp = _T("");
	sSubHeader.Format(_T( "Content-Type: %s%s\r\n"),
		(LPCTSTR)GetContentTypeString(),(LPCTSTR)sTemp);
	sSubHeader += _T("Content-Transfer-Encoding: base64\r\n");
	sTemp.Format( _T("Content-Disposition: attachment; filename=%s%s\r\n"),
		szFName,szExt);
	sSubHeader += sTemp;

	// 设置结束标志
	sSubHeader += _T( "\r\n" ); 

	return sSubHeader;
}

CString CAppOctetStream::GetContentTypeString()
{
	return _T("application/octet-stream");
}

void CAppOctetStream::attach_file(CStdioFile* pFileAtt, 
								  int nEncoding, 
								  CString & sDestination)
{
	CMIMECode* pEncoder;
	int nBytesRead;
	TCHAR szBuffer[BYTES_TO_READ + 1];

	ASSERT( pFileAtt != NULL );
	if( pFileAtt == NULL )
		return;
	switch( nEncoding )
	{
		default:
			break;
		case CMIMEMessage::BASE64:
			try 
			{
				pEncoder = new CBase64;
			}
			catch( CMemoryException* e )
			{
				delete e;
				return;
			}
	}
	if( pEncoder == NULL )
		return;
	do
	{
		try
		{
			nBytesRead = pFileAtt->Read( szBuffer, BYTES_TO_READ );
		}
		catch( CFileException* e )
		{
			delete e;
			break;
		}
		szBuffer[ nBytesRead ] = 0;	
		sDestination += pEncoder->Encode( szBuffer, nBytesRead );
		sDestination += _T( "\r\n" );
	} while( nBytesRead == BYTES_TO_READ );
	sDestination += _T( "\r\n" );
	delete pEncoder;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -