www.pudn.com > ZJMailer > ZJMessage.cpp


// ZJMessage.cpp: implementation of the CZJMessage class. 
// 
////////////////////////////////////////////////////////////////////// 
// CZJMessage & CZJAttachment classes Implementation files 
// 
// Author: ÕÅ ½Ü (codez) 
// 
// Finished At Oct 2nd, 2002. 
// 
//============================================================== 
// note: 
//   The CZJMessage & CZJAttachment classes are made by codez, 
// you can modify and/or distribute this file, but do not remove 
// this header please. 
// 
// This file is provided "as is" with no expressed or implied 
// warranty. 
//============================================================== 
//  
// If there is any bugs, please let me know. 
// Thanks! 
// 
// 
// Contact me: 
// 
// Email: jay_zephyr2002@yahoo.com.cn 
// Home Page: http://www.xlrj.com/codez 
//============================================================== 
// History: 
// 
// * 4th Oct, 2002 : 
//		finished 1st beta version, V0.01 Beta 
// 
// * 5th Oct, 2002 : 
//		fix a bug for content including char 0x2e ('.') 
// 
//============================================================== 
 
#include "stdafx.h" 
#include "ZJMessage.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CZJMessage::CZJMessage() 
{ 
	m_xMailer = _T("ZJMailer 0.1a"); 
	m_contentType = _T("text/plain"); 
	m_contentEncoding = _T("Base64");	// Please do not modify!!! 
	m_boundary = _T("==ZJMailer_V001_codez=="); 
 
	m_attach.Clear(); 
} 
 
CZJMessage::~CZJMessage() 
{ 
	m_attach.Clear(); 
} 
 
////////////////////////////////////////////////////////////////////// 
// CZJAttachment Class 
////////////////////////////////////////////////////////////////////// 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CZJAttachment::CZJAttachment() 
{ 
	m_attach.RemoveAll(); 
} 
 
CZJAttachment::~CZJAttachment() 
{ 
	m_attach.RemoveAll(); 
} 
 
void CZJAttachment::Clear() 
{ 
	m_attach.RemoveAll(); 
} 
 
int CZJAttachment::AddFile(CString &sFile) 
{ 
	ASSERT(sFile.GetLength()); 
 
	return m_attach.Add(sFile); 
} 
 
bool CZJAttachment::DelFile(CString &sFile) 
{ 
	for (int i=0;i=0) && (nIdx<=m_attach.GetUpperBound())); 
	CString tmp = m_attach.GetAt(nIdx); 
 
	int nPos = tmp.ReverseFind('\\'); 
	if (nPos<0) 
	{ 
		tmp.Empty(); 
		return tmp; 
	} 
 
	tmp = tmp.Right(tmp.GetLength()-nPos-1); 
	return tmp; 
} 
 
CString CZJAttachment::GetFileFullName(int nIdx) const 
{ 
	ASSERT( (nIdx>=0) && (nIdx<=m_attach.GetUpperBound())); 
	CString tmp = m_attach.GetAt(nIdx); 
 
	return tmp; 
} 
 
int CZJAttachment::GetCount() const 
{ 
	return m_attach.GetSize(); 
} 
 
CString CZJMessage::GetBody() const 
{ 
	return m_body; 
} 
 
CString CZJMessage::GetFromEmail() const 
{ 
	return m_fromEmail; 
} 
 
CString CZJMessage::GetFromName() const 
{ 
	return m_fromName; 
} 
 
CString CZJMessage::GetSubject() const 
{ 
	return m_subject; 
} 
 
CString CZJMessage::GetTo() const 
{ 
	return m_to; 
} 
 
CString CZJMessage::GetXMailer() const 
{ 
	return m_xMailer; 
} 
 
CString CZJMessage::GetContentType() const 
{ 
	return m_contentType; 
} 
 
CString CZJMessage::GetContentEncoding() const 
{ 
	return m_contentEncoding; 
} 
 
CString CZJMessage::GetCharSet() const 
{ 
	return m_charset; 
} 
 
void CZJMessage::SetBody(CString &body) 
{ 
	m_body = body; 
} 
 
void CZJMessage::SetCharSet(CString &charset) 
{ 
	m_charset = charset; 
} 
 
void CZJMessage::SetFrom(CString &fromName, CString &fromEmail) 
{ 
	m_fromName = fromName; 
	m_fromEmail = fromEmail; 
} 
 
void CZJMessage::SetTo(CString &to) 
{ 
	m_to = to; 
} 
 
void CZJMessage::SetSubject(CString &subject) 
{ 
	m_subject = subject; 
} 
 
void CZJMessage::SetContentType(CString &contentType) 
{ 
	m_contentType = contentType; 
} 
 
CString CZJMessage::GetBoundary() const 
{ 
	return m_boundary; 
} 
 
CString CZJMessage::GetMailHeader() const 
{ 
	CString tmpHeader, tmp, sCRLF; 
	sCRLF = _T("\r\n"); 
 
	ASSERT(!m_fromEmail.IsEmpty()); 
	ASSERT(!m_to.IsEmpty()); 
 
	// Date 
	tmpHeader = _T("Date: "); 
	tmpHeader += GetMailDate(); 
	tmpHeader += sCRLF; 
 
	// From 
	if (m_fromName.IsEmpty()) 
		tmp.Format(_T("From: %s\r\n"), LPCTSTR(m_fromEmail)); 
	else 
		tmp.Format(_T("From: \"%s\"<%s>\r\n"),  
			LPCTSTR(m_fromName), LPCTSTR(m_fromEmail)); 
 
	tmpHeader += tmp; 
 
	// To 
	tmp.Format(_T("To: %s\r\n"), LPCTSTR(m_to));	 
 
	tmpHeader += tmp; 
 
	// Subject 
	/* 
	tmpHeader += _T("Subject: "); 
	tmpHeader += GetMailSubject(); 
	tmpHeader += sCRLF; 
 
	tmp.Format(_T("X-mailer: %s\r\nMime-Version: 1.0\r\n"), 
		LPCTSTR(m_xMailer)); 
 
	tmpHeader += tmp; 
	*/ 
 
	return tmpHeader; 
} 
 
CString CZJMessage::GetMailDate() const 
{ 
	CString tmpDate, tmp; 
 
	SYSTEMTIME st; 
	TCHAR * szWeekOfDay[7]={_T("Sun"), _T("Mon"), 
		_T("Tue"), _T("Wed"), _T("Thu"), _T("Fri"), _T("Sat")}; 
 
	TCHAR * szMonth[13]={NULL, _T("Jan"), _T("Feb"), 
		_T("Mar"), _T("Apr"), _T("May"), _T("Jun"), 
		_T("Jul"), _T("Aug"), _T("Sep"), _T("Oct"), 
		_T("Nov"), _T("Dec") 
	}; 
 
	GetLocalTime(&st); 
 
	if (st.wDayOfWeek<0 || st.wDayOfWeek>6) 
		tmpDate = _T("???"); 
	else 
		tmpDate = szWeekOfDay[st.wDayOfWeek]; 
 
	tmpDate += _T(", "); 
 
	tmp.Format(_T("%d "), st.wDay); 
	tmpDate += tmp; 
 
	if (st.wMonth<1 || st.wMonth>12) 
		tmpDate += _T("???"); 
	else 
		tmpDate += szMonth[st.wMonth]; 
 
	tmp.Format(_T(" %d %d:%d:%d"), st.wYear,  
		st.wHour, st.wMinute, st.wSecond); 
 
	tmpDate += tmp; 
 
	return tmpDate; 
} 
 
bool CZJMessage::NeedEncode(CString & s) 
{ 
	// do not deal with unknow type 
	if (m_charset.GetLength()<=0) 
		return false; 
 
	TCHAR ch; 
 
	if (s.GetAt(0) & 0x80) 
		return true; 
 
	for (int i=1;i