MIMEMessage.cpp
上传用户:weimei12
上传日期:2022-08-11
资源大小:185k
文件大小:5k
- #include "stdafx.h"
- #include "MailMessage.h"
- #include "MIMEContentAgent.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
- // Static Member Initializers
- CMIMEMessage::CMIMETypeManager CMIMEMessage::m_MIMETypeManager;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CMIMEMessage::CMIMEMessage()
- {
- m_strMIMEContentType = _T("multipart/mixed");
- m_strPartBoundary = _T("LX_MAIL_NEXT_PART_10242007");
- m_strNoMIMEText = _T("This is a multi-part message in MIME format.");
- // Register the MIME types handled by this class
- CMIMEContentAgent* pType;
- // These objects are deleted by CMIMTypeManager's destructor
- pType = new CTextPlain(TEXT_PLAIN, GetCharsPerLine());
- RegisterMimeType(pType);
- pType = new CAppOctetStream(APPLICATION_OCTETSTREAM);
- RegisterMimeType(pType);
- }
- CMIMEMessage::~CMIMEMessage()
- {
- }
- ///<summary>
- /// add the part to the part-list used to build the body.
- ///</summary>
- BOOL CMIMEMessage::AddMIMEPart(LPCTSTR szContent,
- int nContentType,
- LPCTSTR szParameters,
- int nEncoding,
- BOOL bPath)
- {
- CMIMEPart part;
- part.m_nContentType = nContentType;
- part.m_strParameters = szParameters;
- part.m_nEncoding = nEncoding;
- part.m_bPath = bPath;
- part.m_strContent = szContent;
- part.m_strContent.TrimLeft();
- part.m_strContent.TrimRight();
- if(TEXT_PLAIN == nContentType)
- {
- m_MIMEPartList.AddHead(part);
- }
- else
- {
- m_MIMEPartList.AddTail(part);
- }
- return TRUE;
- }
- ///<summary>
- /// format the pure header
- ///</summary>
- void CMIMEMessage::PrepareHeader()
- {
- // Let the base class add its headers
- CMailMessage::PrepareHeader();
-
- CString sTemp;
- AddHeaderLine(_T("MIME-Version: 1.0"));
- sTemp.Format(_T("Content-Type: %s; boundary=%s"), m_strMIMEContentType, m_strPartBoundary);
- AddHeaderLine(sTemp);
- }
- ///<summary>
- /// do some jobs if necessary
- ///</summary>
- void CMIMEMessage::PrepareBody()
- {
- // Class user may have assigned body text directly.
- // Convert it to just another MIME part to be processed.
- // If this default Content-Type isn't good enough for the
- // class user, he or she should have used AddMIMEPart() instead.
- if(m_strBody != _T(""))
- AddMIMEPart((LPCTSTR)m_strBody, TEXT_PLAIN, "", _7BIT, FALSE);
- // Initialize the body (replace current contents).
- m_strBody = m_strNoMIMEText;
- m_strBody += _T("rnrn");
- AppendMimeParts();
- InsertMessageEnd(m_strBody);
- CMailMessage::PrepareBody();
- }
- ///<summary>
- /// Insert the boundary part at the end
- ///</summary>
- void CMIMEMessage::InsertMessageEnd(CString& strText)
- {
- CString strTemp;
- if(strText.Right(2) != _T("rn"))
- strText += _T("rn");
- strTemp.Format(_T("--%s--rn"), m_strPartBoundary);
- strText += strTemp;
- }
- ///<summary>
- /// Insert the boundary part
- ///</summary>
- void CMIMEMessage::InsertBoundary(CString& strText)
- {
- CString strTemp;
- if(strText.Right(2) != _T("rn"))
- strText += _T("rn");
- strTemp.Format(_T("--%srn"), m_strPartBoundary);
- strText += strTemp;
- }
- ///<summary>
- /// register one mime type content into the manager
- ///</summary>
- void CMIMEMessage::RegisterMimeType(CMIMEContentAgent* pMIMEType)
- {
- ASSERT(pMIMEType != NULL);
- if(NULL == pMIMEType)
- return;
- m_MIMETypeManager.RegisterMIMEType(pMIMEType);
- }
- ///<summary>
- /// Append each mime part to the body
- ///</summary>
- void CMIMEMessage::AppendMimeParts()
- {
- POSITION partPosition;
- CMIMEPart* pMIMEPart = NULL;
- CMIMEContentAgent* pMIMEType = NULL;
- // Get each part from the list, append them to the body
- partPosition = m_MIMEPartList.GetHeadPosition();
- while(partPosition != NULL)
- {
- pMIMEPart = &m_MIMEPartList.GetNext(partPosition);
- pMIMEType = m_MIMETypeManager.GetHandler(pMIMEPart->m_nContentType);
- if(pMIMEType != NULL)
- {
- InsertBoundary(m_strBody);
- pMIMEType->AppendPart(pMIMEPart->m_strContent,
- pMIMEPart->m_strParameters,
- pMIMEPart->m_nEncoding,
- pMIMEPart->m_bPath,
- m_strBody);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////
- // 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;
- }
- m_csAccess.Unlock();
- }
- ///<summary>
- /// register one mime type content into the manager
- ///</summary>
- void CMIMEMessage::CMIMETypeManager::RegisterMIMEType(CMIMEContentAgent *pMIMEType)
- {
- ASSERT(pMIMEType != NULL);
- if(pMIMEType == NULL)
- return;
- m_csAccess.Lock();
- m_MIMETypeList.AddTail( pMIMEType );
- m_csAccess.Unlock();
- }
- ///<summary>
- /// Get the specified type handler
- ///</summary>
- CMIMEContentAgent* CMIMEMessage::CMIMETypeManager::GetHandler(int nContentType)
- {
- POSITION pos;
- CMIMEContentAgent* pType = NULL;
- m_csAccess.Lock();
- pos = m_MIMETypeList.GetHeadPosition();
- while(pos != NULL)
- {
- pType = m_MIMETypeList.GetNext(pos);
- if(pType->QueryType(nContentType) == TRUE)
- break;
- }
- m_csAccess.Unlock();
- return pType;
- }