📄 mimemessage.cpp
字号:
// MIMEMessage.cpp: implementation of the CMIMEMessage class.
//
#include "stdafx.h"
#include "MIMEMessage.h"
#include "TextPlain.h"
#include "AppOctetStream.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CMIMEMessage::CMIMETypeManager CMIMEMessage::m_MIMETypeManager;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMIMEMessage::CMIMEMessage()
{
m_sMIMEContentType = _T( "multipart/mixed" );
m_sPartBoundary = _T( "WC_MAIL_PaRt_BoUnDaRy_05151998" );
m_sNoMIMEText = _T( "This is a multi-part message in MIME format." );
CMIMEContentAgent* pType;
pType = new CTextPlain( TEXT_PLAIN, GetCharsPerLine() );
register_mime_type( pType );
pType = new CAppOctetStream( APPLICATION_OCTETSTREAM );
register_mime_type( pType );
}
CMIMEMessage::~CMIMEMessage()
{
}
BOOL CMIMEMessage::AddMIMEPart(LPCTSTR szContent,
int nContentType,
LPCTSTR szParameters,
int nEncoding,
BOOL bPath)
{
CMIMEPart MIMEPart;
MIMEPart.m_nContentType = nContentType;
MIMEPart.m_sParameters = szParameters;
MIMEPart.m_nEncoding = nEncoding;
MIMEPart.m_bPath = bPath;
MIMEPart.m_sContent = szContent;
MIMEPart.m_sContent.TrimLeft();
MIMEPart.m_sContent.TrimRight();
if(nContentType == TEXT_PLAIN)
m_MIMEPartList.AddHead(MIMEPart);
else
m_MIMEPartList.AddTail(MIMEPart);
return TRUE;
}
void CMIMEMessage::prepare_header()
{
CString strTemp;
// 由基类的函数来添加邮件头
CMailMessage::prepare_header();
// 这里只添加MIME头
add_header_line(_T("MIME-Version: 1.0"));
strTemp.Format(_T( "Content-Type: %s; boundary=%s" ),
(LPCTSTR)m_sMIMEContentType,
(LPCTSTR)m_sPartBoundary);
add_header_line((LPCTSTR)strTemp);
}
void CMIMEMessage::prepare_body()
{
// 将正文内容加到邮件中
if(m_sBody != _T(""))
AddMIMEPart((LPCTSTR)m_sBody,TEXT_PLAIN,"",_7BIT,FALSE );
// 初始化MIME体
m_sBody = m_sNoMIMEText;
m_sBody += _T("\r\n\r\n");
// 加入附件的内容
append_mime_parts();
insert_message_end(m_sBody);
// 基类函数为正文加上结束符
CMailMessage::prepare_body();
}
void CMIMEMessage::insert_boundary(CString& strText)
{
CString strTemp;
if(strText.Right(2) != _T("\r\n"))
strText += _T("\r\n");
strTemp.Format(_T("--%s\r\n"),(LPCTSTR)m_sPartBoundary);
strText += strTemp;
}
void CMIMEMessage::insert_message_end(CString& strText)
{
CString strTemp;
if( strText.Right(2) != _T("\r\n"))
strText += _T("\r\n");
strTemp.Format(_T("--%s--\r\n"),(LPCTSTR)m_sPartBoundary);
strText += strTemp;
}
void CMIMEMessage::register_mime_type(CMIMEContentAgent* pMIMEType)
{
ASSERT( pMIMEType != NULL );
if( pMIMEType == NULL )
return;
m_MIMETypeManager.RegisterMIMEType( pMIMEType );
}
void CMIMEMessage::append_mime_parts()
{
CMIMEPart* pMIMEPart = NULL;
CMIMEContentAgent* pMIMEType = NULL;
POSITION pos = m_MIMEPartList.GetHeadPosition();
while (pos)
{
pMIMEPart = &m_MIMEPartList.GetNext(pos);
pMIMEType = m_MIMETypeManager.GetHandler(pMIMEPart->m_nContentType);
// 根据附件的类型分别处理
if (pMIMEType)
{
insert_boundary(m_sBody);
pMIMEType->AppendPart(pMIMEPart->m_sContent,pMIMEPart->m_sParameters,
pMIMEPart->m_nEncoding,pMIMEPart->m_bPath,m_sBody);
}
}
}
//////////////////////////////////////////////////////////////////////
// CMIMETypeManager Implementation
//////////////////////////////////////////////////////////////////////
CMIMEMessage::CMIMETypeManager::CMIMETypeManager()
{
}
CMIMEMessage::CMIMETypeManager::~CMIMETypeManager()
{
POSITION pos;
CMIMEContentAgent* p;
m_csAccess.Lock();
pos = m_MIMETypeList.GetHeadPosition();
while( pos != NULL )
{
p = m_MIMETypeList.GetNext( pos );
delete p;
}
}
void CMIMEMessage::CMIMETypeManager::RegisterMIMEType(CMIMEContentAgent *pMIMEType)
{
ASSERT( pMIMEType != NULL );
if( pMIMEType == NULL )
return;
m_csAccess.Lock();
m_MIMETypeList.AddTail( pMIMEType );
}
CMIMEContentAgent* CMIMEMessage::CMIMETypeManager::GetHandler(int nContentType)
{
POSITION pos;
CMIMEContentAgent* pType = NULL;
m_csAccess.Lock();
pos = m_MIMETypeList.GetHeadPosition();
while (pos)
{
pType = m_MIMETypeList.GetNext(pos);
if (pType->QueryType(nContentType))
break;
}
return pType;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -