📄 pjnsmtp.cpp
字号:
szDest[nU8] = static_cast<char>(*pwszSrc);
}
nU8++;
}
else if (*pwszSrc <= 0x07ff)
{
//Found 2 byte sequence if < 0x07ff (11 bits).
if (nDest)
{
if ((nU8 + 1) < nDest)
{
//Use upper 5 bits in first byte.
//Use lower 6 bits in second byte.
szDest[nU8++] = static_cast<char>(0xc0 | (*pwszSrc >> 6));
szDest[nU8++] = static_cast<char>(0x80 | (*pwszSrc & 0x003f));
}
else
{
//Error - buffer too small.
nSrc++;
break;
}
}
else
{
nU8 += 2;
}
}
else
{
//Found 3 byte sequence.
if (nDest)
{
if ((nU8 + 2) < nDest)
{
//Use upper 4 bits in first byte.
//Use middle 6 bits in second byte.
//Use lower 6 bits in third byte.
szDest[nU8++] = static_cast<char>(0xe0 | (*pwszSrc >> 12));
szDest[nU8++] = static_cast<char>(0x80 | (((*pwszSrc) & 0x0fc0) >> 6));
szDest[nU8++] = static_cast<char>(0x80 | (*pwszSrc & 0x003f));
}
else
{
//Error - buffer too small.
nSrc++;
break;
}
}
else
{
nU8 += 3;
}
}
}
pwszSrc++;
}
//If the last character was a high surrogate, then handle it as a normal unicode character.
if ((nSrc < 0) && (wchHighSurrogate != 0))
{
if (nDest)
{
if ((nU8 + 2) < nDest)
{
szDest[nU8++] = static_cast<char>(0xe0 | (wchHighSurrogate >> 12));
szDest[nU8++] = static_cast<char>(0x80 | (((wchHighSurrogate) & 0x0fc0) >> 6));
szDest[nU8++] = static_cast<char>(0x80 | (wchHighSurrogate & 0x003f));
}
else
{
nSrc++;
}
}
}
//Make sure the destination buffer was large enough.
if (nDest && (nSrc >= 0))
{
return 0;
}
//Return the number of UTF-8 characters written.
return nU8;
}
void CPJNSMTPBodyPart::FreeHeader(LPSTR& pszHeader)
{
//The CPJNSMTPBodyPart class always allocates the memory for the header
delete [] pszHeader;
pszHeader = NULL;
}
void CPJNSMTPBodyPart::FreeBody(LPSTR& pszBody)
{
//The CPJNSMTPBodyPart class allocates the memory for the body if it was not base 64 encoded
delete [] pszBody;
pszBody = NULL;
}
void CPJNSMTPBodyPart::FreeFooter(LPSTR& pszFooter)
{
//The CPJNSMTPBodyPart class always allocates the memory for the footer
delete [] pszFooter;
pszFooter = NULL;
}
BOOL CPJNSMTPBodyPart::GetHeader(LPSTR& pszHeader, int& nHeaderSize)
{
USES_CONVERSION;
//Assume the worst
BOOL bSuccess = FALSE;
CString sHeader;
if (m_sFilename.GetLength())
{
//Ok, it's a file
//Form the header to go along with this body part
if (GetNumberOfChildBodyParts())
sHeader.Format(_T("\r\n\r\n--%s\r\nContent-Type: %s; name=\"%s\"; Boundary=\"%s\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"%s\"\r\n"),
m_pParentBodyPart->m_sBoundary.operator LPCTSTR(), m_sContentType.operator LPCTSTR(), m_sTitle.operator LPCTSTR(), m_sBoundary.operator LPCTSTR(), m_sTitle.operator LPCTSTR());
else
sHeader.Format(_T("\r\n\r\n--%s\r\nContent-Type: %s; name=\"%s\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"%s\"\r\n"),
m_pParentBodyPart->m_sBoundary.operator LPCTSTR(), m_sContentType.operator LPCTSTR(), m_sTitle.operator LPCTSTR(), m_sTitle.operator LPCTSTR());
bSuccess = TRUE;
}
else
{
//ok, it's some text
//Form the header to go along with this body part
ASSERT(m_pParentBodyPart);
if (GetNumberOfChildBodyParts())
{
if (m_bBase64)
sHeader.Format(_T("\r\n--%s\r\nContent-Type: %s; charset=%s; Boundary=\"%s\"\r\nContent-Transfer-Encoding: base64\r\n"),
m_pParentBodyPart->m_sBoundary.operator LPCTSTR(), m_sContentType.operator LPCTSTR(), m_sCharset.operator LPCTSTR(), m_sBoundary.operator LPCTSTR());
else if (m_bQuotedPrintable)
sHeader.Format(_T("\r\n--%s\r\nContent-Type: %s; charset=%s; Boundary=\"%s\"\r\nContent-Transfer-Encoding: quoted-printable\r\n"),
m_pParentBodyPart->m_sBoundary.operator LPCTSTR(), m_sContentType.operator LPCTSTR(), m_sCharset.operator LPCTSTR(), m_sBoundary.operator LPCTSTR());
else
sHeader.Format(_T("\r\n--%s\r\nContent-Type: %s; charset=%s; Boundary=\"%s\"\r\n"),
m_pParentBodyPart->m_sBoundary.operator LPCTSTR(), m_sContentType.operator LPCTSTR(), m_sCharset.operator LPCTSTR(), m_sBoundary.operator LPCTSTR());
}
else
{
if (m_bBase64)
sHeader.Format(_T("\r\n--%s\r\nContent-Type: %s; charset=%s\r\nContent-Transfer-Encoding: base64\r\n"),
m_pParentBodyPart->m_sBoundary.operator LPCTSTR(), m_sContentType.operator LPCTSTR(), m_sCharset.operator LPCTSTR());
else if (m_bQuotedPrintable)
sHeader.Format(_T("\r\n--%s\r\nContent-Type: %s; charset=%s\r\nContent-Transfer-Encoding: quoted-printable\r\n"),
m_pParentBodyPart->m_sBoundary.operator LPCTSTR(), m_sContentType.operator LPCTSTR(), m_sCharset.operator LPCTSTR());
else
sHeader.Format(_T("\r\n--%s\r\nContent-Type: %s; charset=%s\r\n"),
m_pParentBodyPart->m_sBoundary.operator LPCTSTR(), m_sContentType.operator LPCTSTR(), m_sCharset.operator LPCTSTR());
}
bSuccess = TRUE;
}
//Add the other headers
if (m_sContentBase.GetLength())
{
CString sLine;
sLine.Format(_T("Content-Base: %s\r\n"), m_sContentBase.operator LPCTSTR());
sHeader += sLine;
}
if (m_sContentID.GetLength())
{
CString sLine;
sLine.Format(_T("Content-ID: %s\r\n"), m_sContentID.operator LPCTSTR());
sHeader += sLine;
}
if (m_sContentLocation.GetLength())
{
CString sLine;
sLine.Format(_T("Content-Location: %s\r\n"), m_sContentLocation.operator LPCTSTR());
sHeader += sLine;
}
sHeader += _T("\r\n");
nHeaderSize = static_cast<int>(_tcslen(sHeader));
pszHeader = new char[nHeaderSize+1];
#if (_MSC_VER >= 1400)
strcpy_s(pszHeader, nHeaderSize+1, T2A(const_cast<LPTSTR>(sHeader.operator LPCTSTR())));
#else
strcpy(pszHeader, T2A(const_cast<LPTSTR>(sHeader.operator LPCTSTR())));
#endif
return bSuccess;
}
BOOL CPJNSMTPBodyPart::GetBody(BOOL bDoSingleDotFix, LPSTR& pszBody, int& nBodySize)
{
//if the body is text we must convert it to the declared encoding, this could create some
//problems since windows conversion functions (such as WideCharToMultiByte) uses UINT
//code page and not a String like HTTP uses.
//For now we will add the support for UTF-8, to support other encodings we have to
//implement a mapping between the "internet name" of the encoding and its LCID(UINT)
USES_CONVERSION;
//Assume the worst
BOOL bSuccess = FALSE;
if (m_sFilename.GetLength())
{
//Ok, it's a file
HANDLE hFile = CreateFile(m_sFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
//Get the length of the file (we only support sending files less than m_dwMaxAttachmentSize)
DWORD dwFileSizeHigh = 0;
DWORD dwFileSizeLow = GetFileSize(hFile, &dwFileSizeHigh);
if (dwFileSizeLow != INVALID_FILE_SIZE)
{
if (dwFileSizeLow)
{
if (dwFileSizeLow <= m_dwMaxAttachmentSize)
{
//read in the contents of the input file
BYTE* pszIn = new BYTE[dwFileSizeLow];
//Read in the contents of the file
DWORD dwBytesWritten = 0;
if (ReadFile(hFile, pszIn, dwFileSizeLow, &dwBytesWritten, NULL))
{
//Do the encoding
m_Coder.Encode(pszIn, dwBytesWritten, BASE64_FLAG_NONE);
//Form the body for this body part
nBodySize = m_Coder.ResultSize();
pszBody = new char[nBodySize + 1];
#if (_MSC_VER >= 1400)
strcpy_s(pszBody, nBodySize+1, m_Coder.Result());
#else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -