TextPlain.cpp
上传用户:weimei12
上传日期:2022-08-11
资源大小:185k
文件大小:3k
- #include "stdafx.h"
- #include "MIMEContentAgent.h"
- #include "TextPlain.h"
- #include "MailMessage.h"
- #include "MIMEMessage.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CTextPlain::CTextPlain(int nContentType, UINT nWrapPos)
- :CMIMEContentAgent(nContentType)
- {
- m_nWrapPos = nWrapPos;
- }
- CTextPlain::~CTextPlain()
- {
- }
- ///<summary>
- /// Get the content type sting
- ///</summary>
- CString CTextPlain::GetContentTypeString()
- {
- CString s;
- s = _T("text/plain");
- return s;
- }
- ///<summary>
- /// append it to the body
- ///</summary>
- BOOL CTextPlain::AppendPart(LPCTSTR szContent,
- LPCTSTR szParameters,
- int nEncoding,
- BOOL bPath,
- CString & strDestination)
- {
- CString strSubHeader;
- CString strWrapped;
- strSubHeader = BuildSubHeader(szContent, szParameters, nEncoding, bPath);
- strWrapped = WrapText(szContent);
- strDestination += (strSubHeader + strWrapped);
- return TRUE;
- }
- ///<summary>
- /// construct the sub-part header
- ///</summary>
- CString CTextPlain::BuildSubHeader(LPCTSTR szContent,
- LPCTSTR szParameters,
- int nEncoding,
- BOOL bPath)
- {
- CString strSubHeader;
- strSubHeader.Format(_T("Content-Type: %s%srn"), GetContentTypeString(), szParameters);
- strSubHeader += _T("Content-Transfer-Encoding: ");
- switch(nEncoding)
- {
- // only 7bit encoding
- default:
- case CMIMEMessage::_7BIT:
- strSubHeader += _T("7bit");
- }
- strSubHeader += _T("rnrn");
- return strSubHeader;
- }
- ///<summary>
- /// wrap the content
- ///</summary>
- CString CTextPlain::WrapText(LPCTSTR szText)
- {
- CString strTemp;
- ASSERT(szText != NULL);
- if(NULL == szText)
- {
- return strTemp;
- }
- CString strLeft;
- CString strRight;
- int lp = 0;
- UINT nCount = 0;
- int nSpacePos = 0;
- strTemp = szText;
- while(lp < strTemp.GetLength())
- {
- if(strTemp[lp] == ' ')
- {
- nSpacePos = lp;
- }
- // Reset the counter on a newline
- if(strTemp.Mid(lp, 2) == _T("rn"))
- {
- nCount = 0;
- }
- // Wrap text at last found space,change the space to rn
- if(nCount > m_nWrapPos)
- {
- strLeft = strTemp.Left(nSpacePos);
- strRight = strTemp.Right(strTemp.GetLength() - nSpacePos);
- strLeft.TrimRight();
- strRight.TrimLeft();
- strLeft += _T("rn");
- strTemp = strLeft + strRight;
- nCount = 0;
- }
- else
- {
- nCount++;
- }
- lp++;
- }
- return strTemp;
- }