MailMessage.cpp
上传用户:weimei12
上传日期:2022-08-11
资源大小:185k
文件大小:6k
- #include "stdafx.h"
- #include "MailMessage.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CMailMessage::CMailMessage()
- {
- SetCharsPerLine(MAIL_CHARS_PER_LINE);
- }
- CMailMessage::~CMailMessage()
- {
- }
- ///<summary>
- /// Add a recipient's information
- ///</summary>
- BOOL CMailMessage::AddRecipient(const CString& strEmailAddress, const CString& strFriendlyName, RECIPIENTS_TYPE type)
- {
- CRecipient to;
- to.m_strEmailAddress = strEmailAddress;
- to.m_strFriendlyName = strFriendlyName;
- switch(type)
- {
- case TO:
- m_Recipients.Add(to);
- break;
- case CC:
- m_CCRecipients.Add(to);
- break;
- case BCC:
- m_BCCRecipients.Add(to);
- break;
- default:
- ASSERT(FALSE);
- return FALSE;
- }
- return TRUE;
- }
- ///<summary>
- /// Get a recipient's information
- ///</summary>
- BOOL CMailMessage::GetRecipient(CString& strEmailAddress, CString& strFriendlyName, int nIndex, RECIPIENTS_TYPE type)
- {
- if(nIndex < 0)
- {
- return FALSE;
- }
- int nMax = (int)max(m_Recipients.GetUpperBound(), m_CCRecipients.GetUpperBound());
- if (nIndex > max(nMax, m_BCCRecipients.GetUpperBound()))
- {
- return FALSE;
- }
-
- CRecipient to;
- switch(type)
- {
- case TO:
- to = m_Recipients[nIndex];
- break;
- case CC:
- to = m_CCRecipients[nIndex];
- break;
- case BCC:
- to = m_BCCRecipients[nIndex];
- break;
- default:
- ASSERT(FALSE);
- return FALSE;
- }
- strEmailAddress = to.m_strEmailAddress;
- strFriendlyName = to.m_strFriendlyName;
- return TRUE;
- }
- ///<summary>
- /// Get the count of recipients
- ///</summary>
- int CMailMessage::GetNumRecipients(RECIPIENTS_TYPE type)
- {
- int nNumber = 0;
- switch(type)
- {
- case TO:
- nNumber = (int)m_Recipients.GetSize();
- break;
- case CC:
- nNumber = (int)m_CCRecipients.GetSize();
- break;
- case BCC:
- nNumber = (int)m_BCCRecipients.GetSize();
- break;
- default:
- ASSERT(FALSE);
- return 0;
- }
- return nNumber;
- }
- ///<summary>
- /// Add multiple recipient's information
- ///</summary>
- BOOL CMailMessage::AddMultipleRecipients(LPCTSTR szRecipients, RECIPIENTS_TYPE type)
- {
- ASSERT(szRecipients != NULL);
- if (NULL == szRecipients)
- {
- return FALSE;
- }
- TCHAR* pszBuf = NULL;
- UINT nPos = 0;
- UINT nStart = 0;
- CString strTemp;
- CString strEmail;
- CString strFriendly;
- UINT nLength = 0;
- int nMark = -1;
- int nMark2 = -1;
- // Add Recipients one by one
- nLength = (int)_tcslen(szRecipients);
- pszBuf = new TCHAR[nLength + 1]; // Allocate a temporary area for operating
- _tcscpy_s(pszBuf, nLength + 1, szRecipients);
- for(; nPos <= nLength; nPos++)
- {
- if(pszBuf[nPos] == ';' || pszBuf[nPos] == 0)
- {
- // First, pick apart the sub-strings (separated by ';') and store it in strTemp.
- pszBuf[nPos] = 0;
- strTemp = &pszBuf[nStart];
- // Now divide the substring into friendly names and email addresses.
- nMark = strTemp.Find('<');
- if(nMark >= 0)
- {
- strFriendly = strTemp.Left(nMark);
- nMark2 = strTemp.Find('>');
- if(nMark2 < nMark)
- {
- delete[] pszBuf;
- pszBuf = NULL;
- return FALSE;
- }
- // End of mark at closing bracket or end of string
- strEmail = strTemp.Mid(nMark + 1, nMark2 - (nMark + 1));
- }
- else
- {
- strEmail = strTemp;
- strFriendly = _T( "" );
- }
- AddRecipient(strEmail, strFriendly, type);
- nStart = nPos + 1;
- }
- }
- delete[] pszBuf;
- pszBuf = NULL;
- return TRUE;
- }
- ///<summary>
- /// get and set chars per line
- ///</summary>
- void CMailMessage::SetCharsPerLine(UINT nCharsPerLine)
- {
- m_nCharsPerLine = nCharsPerLine;
- }
- UINT CMailMessage::GetCharsPerLine()
- {
- return m_nCharsPerLine;
- }
- ///<summary>
- /// set the related inforamtion
- ///</summary>
- void CMailMessage::SetInfo(const CString& strFrom, const CString& strSubject, const CString& strBody,
- const CString& strUsername, const CString& strPassword)
- {
- m_strFrom = strFrom;
- m_strSubject = strSubject;
- m_strBody = strBody;
- m_strUsername = strUsername;
- m_strPassword = strPassword;
- }
- ///<summary>
- /// get the mail's body
- ///</summary>
- CString CMailMessage::GetMailBody()
- {
- return m_strBody;
- }
- ///<summary>
- /// get the mail's header
- ///</summary>
- CString CMailMessage::GetMailHeader()
- {
- return m_strHeader;
- }
- ///<summary>
- /// format the mail' header without any encoding mechanism
- ///</summary>
- void CMailMessage::FormatMessage()
- {
- m_strHeader = _T("");
- PrepareHeader();
- m_strHeader += _T("rn");
- PrepareBody();
- }
- ///<summary>
- /// format the pure header
- ///</summary>
- void CMailMessage::PrepareHeader()
- {
- CString strTemp;
- // Date:
- m_tDateTime = m_tDateTime.GetCurrentTime();
- // Format: Mon, 01 Jun 98 01:10:30 GMT
- strTemp = _T("Date: ");
- strTemp += m_tDateTime.Format("%a, %d %b %Y %H:%M:%S %Z");
- AddHeaderLine(strTemp);
- // From:
- strTemp = _T("From: ") + m_strFrom;
- AddHeaderLine((LPCTSTR)strTemp);
- // Subject:
- strTemp = _T("Subject: ") + m_strSubject;
- AddHeaderLine(strTemp);
- // To:
- strTemp = _T("To: ");
- CString strEmail;
- CString strFriendly;
- int i = 0;
- for(; i < GetNumRecipients(); i++)
- {
- GetRecipient(strEmail, strFriendly, i);
- strTemp += (i > 0 ? _T(",") : _T(""));
- strTemp += strFriendly;
- strTemp += _T("<");
- strTemp += strEmail;
- strTemp += _T(">");
- }
- AddHeaderLine(strTemp);
- strTemp = _T("Cc: ");
- strEmail.Empty();
- strFriendly.Empty();
- for(i = 0; i < GetNumRecipients(CC); i++)
- {
- GetRecipient(strEmail, strFriendly, i, CC);
- strTemp += (i > 0 ? _T(",") : _T(""));
- strTemp += strFriendly;
- strTemp += _T("<");
- strTemp += strEmail;
- strTemp += _T(">");
- }
- AddHeaderLine(strTemp);
- }
- ///<summary>
- /// do some jobs if necessary
- ///</summary>
- void CMailMessage::PrepareBody()
- {
- // Append a CR/LF to body if necessary.
- if(m_strBody.Right(2) != _T("rn"))
- m_strBody += _T("rn");
- }
- ///<summary>
- /// add a line into the mail's header
- ///</summary>
- void CMailMessage::AddHeaderLine(const CString& strHeaderLine)
- {
- CString strTemp;
- strTemp.Format(_T( "%srn"), strHeaderLine);
- m_strHeader += strTemp;
- }