📄 pjnsmtp.cpp
字号:
void CPJNSMPTBase64::Decode(LPCSTR pszMessage)
{
Decode(pszMessage, static_cast<int>(strlen(pszMessage)));
}
CPJNSMTPException::CPJNSMTPException(HRESULT hr, const CString& sLastResponse) : m_hr(hr),
m_sLastResponse(sLastResponse)
{
}
CPJNSMTPException::CPJNSMTPException(DWORD dwError, DWORD dwFacility, const CString& sLastResponse) : m_hr(MAKE_HRESULT(SEVERITY_ERROR, dwFacility, dwError)),
m_sLastResponse(sLastResponse)
{
}
BOOL CPJNSMTPException::GetErrorMessage(LPTSTR pstrError, UINT nMaxError, PUINT pnHelpContext)
{
ASSERT(pstrError != NULL && AfxIsValidString(pstrError, nMaxError));
if (pnHelpContext != NULL)
*pnHelpContext = 0;
BOOL bRet = FALSE;
if (HRESULT_FACILITY(m_hr) == FACILITY_ITF)
{
//Simply load up the string from the string table
CString sError;
AfxFormatString1(sError, HRESULT_CODE(m_hr), m_sLastResponse);
lstrcpyn(pstrError, sError, nMaxError);
bRet = TRUE;
}
else
{
LPTSTR lpBuffer;
bRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, HRESULT_CODE(m_hr), MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
reinterpret_cast<LPTSTR>(&lpBuffer), 0, NULL);
if (bRet == FALSE)
*pstrError = '\0';
else
{
lstrcpyn(pstrError, lpBuffer, nMaxError);
LocalFree(lpBuffer);
}
}
return bRet;
}
CString CPJNSMTPException::GetErrorMessage()
{
CString rVal;
LPTSTR pstrError = rVal.GetBuffer(4096);
GetErrorMessage(pstrError, 4096, NULL);
rVal.ReleaseBuffer();
return rVal;
}
IMPLEMENT_DYNAMIC(CPJNSMTPException, CException)
#ifdef _DEBUG
void CPJNSMTPException::Dump(CDumpContext& dc) const
{
//Let the base class do its thing
CObject::Dump(dc);
dc << "m_hr = " << m_hr;
}
#endif
CPJNSMTPAddress::CPJNSMTPAddress()
{
}
CPJNSMTPAddress::CPJNSMTPAddress(const CPJNSMTPAddress& address)
{
*this = address;
}
CPJNSMTPAddress::CPJNSMTPAddress(const CString& sAddress)
{
//The local variable which we will operate on
CString sTemp(sAddress);
sTemp.TrimLeft();
sTemp.TrimRight();
//divide the substring into friendly names and e-mail addresses
int nMark = sTemp.Find(_T('<'));
int nMark2 = sTemp.Find(_T('>'));
if ((nMark != -1) && (nMark2 != -1) && (nMark2 > (nMark+1)))
{
m_sEmailAddress = sTemp.Mid(nMark+1, nMark2 - nMark - 1);
m_sFriendlyName = sTemp.Left(nMark);
m_sFriendlyName.TrimLeft();
m_sFriendlyName.TrimRight();
}
else
{
nMark = sTemp.Find(_T('('));
nMark2 = sTemp.Find(_T(')'));
if ((nMark != -1) && (nMark2 != -1) && (nMark2 > (nMark+1)))
{
m_sEmailAddress = sTemp.Left(nMark);
m_sEmailAddress.TrimRight();
m_sFriendlyName = sTemp.Mid(nMark+1, nMark2 - nMark - 1);
m_sFriendlyName.TrimLeft();
m_sFriendlyName.TrimRight();
}
else
m_sEmailAddress = sTemp;
}
}
CPJNSMTPAddress::CPJNSMTPAddress(const CString& sFriendly, const CString& sAddress) : m_sFriendlyName(sFriendly),
m_sEmailAddress(sAddress)
{
ASSERT(m_sEmailAddress.GetLength()); //An empty address is not allowed
}
CPJNSMTPAddress& CPJNSMTPAddress::operator=(const CPJNSMTPAddress& r)
{
m_sFriendlyName = r.m_sFriendlyName;
m_sEmailAddress = r.m_sEmailAddress;
return *this;
}
CString CPJNSMTPAddress::GetRegularFormat(BOOL bEncode, const CString& sCharset) const
{
ASSERT(m_sEmailAddress.GetLength()); //Email Address must be valid
CString sAddress;
if (m_sFriendlyName.IsEmpty())
sAddress = m_sEmailAddress; //Just transfer the address across directly
else
{
if (bEncode)
{
std::string sAsciiEncodedFriendly = CPJNSMTPBodyPart::HeaderEncode(m_sFriendlyName, sCharset);
CString sEncodedFriendly(sAsciiEncodedFriendly.c_str());
sAddress.Format(_T("%s <%s>"), sEncodedFriendly.operator LPCTSTR(), m_sEmailAddress.operator LPCTSTR());
}
else
sAddress.Format(_T("%s <%s>"), m_sFriendlyName.operator LPCTSTR(), m_sEmailAddress.operator LPCTSTR());
}
return sAddress;
}
CPJNSMTPBodyPart::CPJNSMTPBodyPart() : m_sCharset(_T("iso-8859-1")),
m_sContentType(_T("text/plain")),
m_pParentBodyPart(NULL),
m_bQuotedPrintable(TRUE),
m_bBase64(FALSE),
m_dwMaxAttachmentSize(52428800)
{
//Automatically generate a unique boundary separator for this body part by creating a guid
UUID uuid = { 0 };
UuidCreate(&uuid);
//Convert it to a string
#ifdef _UNICODE
unsigned short* pszGuid = NULL;
#else
unsigned char* pszGuid = NULL;
#endif
UuidToString(&uuid, &pszGuid);
m_sBoundary = reinterpret_cast<TCHAR*>(pszGuid);
//Free up the temp memory
RpcStringFree(&pszGuid);
}
CPJNSMTPBodyPart::CPJNSMTPBodyPart(const CPJNSMTPBodyPart& bodyPart)
{
*this = bodyPart;
}
CPJNSMTPBodyPart::~CPJNSMTPBodyPart()
{
//Free up the array memory
for (int i=0; i<m_ChildBodyParts.GetSize(); i++)
delete m_ChildBodyParts.GetAt(i);
m_ChildBodyParts.RemoveAll();
}
CPJNSMTPBodyPart& CPJNSMTPBodyPart::operator=(const CPJNSMTPBodyPart& bodyPart)
{
m_sFilename = bodyPart.m_sFilename;
m_sText = bodyPart.m_sText;
m_sTitle = bodyPart.m_sTitle;
m_sContentType = bodyPart.m_sContentType;
m_sCharset = bodyPart.m_sCharset;
m_sContentBase = bodyPart.m_sContentBase;
m_sContentID = bodyPart.m_sContentID;
m_sContentLocation = bodyPart.m_sContentLocation;
m_pParentBodyPart = bodyPart.m_pParentBodyPart;
m_sBoundary = bodyPart.m_sBoundary;
m_bQuotedPrintable = bodyPart.m_bQuotedPrintable;
m_bBase64 = bodyPart.m_bBase64;
m_dwMaxAttachmentSize = bodyPart.m_dwMaxAttachmentSize;
//Free up the array memory
int i;
for (i=0; i<m_ChildBodyParts.GetSize(); i++)
delete m_ChildBodyParts.GetAt(i);
m_ChildBodyParts.RemoveAll();
//Now copy over the new object
for (i=0; i<bodyPart.m_ChildBodyParts.GetSize(); i++)
{
CPJNSMTPBodyPart* pBodyPart = new CPJNSMTPBodyPart(*bodyPart.m_ChildBodyParts.GetAt(i));
pBodyPart->m_pParentBodyPart = this;
m_ChildBodyParts.Add(pBodyPart);
}
return *this;
}
BOOL CPJNSMTPBodyPart::SetFilename(const CString& sFilename)
{
ASSERT(sFilename.GetLength()); //Empty Filename !
//Hive away the filename and form the title from the filename
TCHAR sPath[_MAX_PATH];
TCHAR sFname[_MAX_FNAME];
TCHAR sExt[_MAX_EXT];
#if (_MSC_VER >= 1400)
_tsplitpath_s(sFilename, NULL, 0, NULL, 0, sFname, sizeof(sFname)/sizeof(TCHAR), sExt, sizeof(sExt)/sizeof(TCHAR));
_tmakepath_s(sPath, sizeof(sPath)/sizeof(TCHAR), NULL, NULL, sFname, sExt);
#else
_tsplitpath(sFilename, NULL, NULL, sFname, sExt);
_tmakepath(sPath, NULL, NULL, sFname, sExt);
#endif
m_sFilename = sFilename;
m_sTitle = sPath;
//Also sent the content type to be appropiate for an attachment
m_sContentType = _T("application/octet-stream");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -