www.pudn.com > MailAgent.rar > MailMessage.cpp
#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()
{
m_sBody=_T("");
m_sHeader=_T("");
}
CMailMessage::~CMailMessage()
{
}
BOOL CMailMessage::AddRecipient( LPCTSTR szEmailAddress, LPCTSTR szFriendlyName)
{
ASSERT( szEmailAddress != NULL );
ASSERT( szFriendlyName != NULL );
CRecipient to;
to.m_sEmailAddress = szEmailAddress;
to.m_sFriendlyName = szFriendlyName;
m_Recipients.Add( to );
return TRUE;
}
BOOL CMailMessage::GetRecipient(CString & sEmailAddress, CString & sFriendlyName, int nIndex)
{
CRecipient to;
if( nIndex < 0 || nIndex > m_Recipients.GetUpperBound() )
return FALSE;
to = m_Recipients[ nIndex ];
sEmailAddress = to.m_sEmailAddress;
sFriendlyName = to.m_sFriendlyName;
return TRUE;
}
int CMailMessage::GetNumRecipients()
{
return m_Recipients.GetSize();
}
BOOL CMailMessage::AddMultipleRecipients(LPCTSTR szRecipients )
{
TCHAR* buf;
UINT pos;
UINT start;
CString sTemp;
CString sEmail;
CString sFriendly;
UINT length;
int nMark;
int nMark2;
ASSERT( szRecipients != NULL );
length = strlen( szRecipients );
buf = new TCHAR[ length + 1 ];
strcpy( buf, szRecipients );
for( pos = 0, start = 0; pos <= length; pos++ )
{
if( buf[ pos ] == ';' ||
buf[ pos ] == 0 )
{
buf[ pos ] = 0;
sTemp = &buf[ start ];
nMark = sTemp.Find( '<' );
if( nMark >= 0 )
{
sFriendly = sTemp.Left( nMark );
nMark2 = sTemp.Find( '>' );
if( nMark2 < nMark )
{
delete[] buf;
return FALSE;
}
nMark2 > -1 ? nMark2 = nMark2 : nMark2 = sTemp.GetLength() - 1;
sEmail = sTemp.Mid( nMark + 1, nMark2 - (nMark + 1) );
}
else
{
sEmail = sTemp;
sFriendly = _T( "" );
}
AddRecipient( sEmail, sFriendly );
start = pos + 1;
}
}
delete[] buf;
return TRUE;
}
void CMailMessage::FormatMessage()
{
start_header();
prepare_header();
end_header();
prepare_body();
}
void CMailMessage::SetCharsPerLine(UINT nCharsPerLine)
{
m_nCharsPerLine = nCharsPerLine;
}
UINT CMailMessage::GetCharsPerLine()
{
return m_nCharsPerLine;
}
void CMailMessage::prepare_header()
{
CString sTemp;
sTemp = _T( "" );
sTemp = _T( "寄信人地址: " ) + m_sFrom;
add_header_line( (LPCTSTR)sTemp );
sTemp = _T( "收信人地址: " );
CString sEmail = _T( "" );
CString sFriendly = _T( "" );
for( int i = 0; i < GetNumRecipients(); i++ )
{
GetRecipient( sEmail, sFriendly, i );
sTemp += ( i > 0 ? _T( "," ) : _T( "" ) );
sTemp += sFriendly;
sTemp += _T( "<" );
sTemp += sEmail;
sTemp += _T( ">" );
}
add_header_line( (LPCTSTR)sTemp );
// 日期
m_tDateTime = m_tDateTime.GetCurrentTime();
sTemp = _T( "日期: " );
sTemp += m_tDateTime.Format( "%a, %d %b %y %H:%M:%S %Z" );
add_header_line( (LPCTSTR)sTemp );
sTemp = _T( "主题: " ) + m_sSubject;
add_header_line( (LPCTSTR)sTemp );
}
void CMailMessage::prepare_body()
{
// 需要时,添加CR/LF
if( m_sBody.Right( 2 ) != _T( "\r\n" ) )
m_sBody += _T( "\r\n" );
}
void CMailMessage::start_header()
{
m_sHeader = _T( "" );
}
void CMailMessage::end_header()
{
m_sHeader += _T( "\r\n" );
}
void CMailMessage::add_header_line(LPCTSTR szHeaderLine)
{
CString sTemp;
sTemp.Format( _T( "%s\r\n" ), szHeaderLine );
m_sHeader += sTemp;
}
BOOL CMailMessage::DecodeHeader()
{
int startpos, endpos;
CString sSearchFor;
//We can assume that there's a CR/LF before each of the tags, as the servers insert
//Received: lines on top of the mail while transporting the mail
sSearchFor="\r\nFrom: ";
startpos=m_sHeader.Find(sSearchFor);
if (startpos<0) return FALSE;
endpos=m_sHeader.Mid(startpos+sSearchFor.GetLength()).Find("\r\n");
m_sFrom=m_sHeader.Mid(startpos+sSearchFor.GetLength(),endpos);
sSearchFor="\r\nTo: ";
startpos=m_sHeader.Find(sSearchFor);
if (startpos<0) return FALSE;
endpos=m_sHeader.Mid(startpos+sSearchFor.GetLength()).Find("\r\n");
AddMultipleRecipients(m_sHeader.Mid(startpos+sSearchFor.GetLength(),endpos));
sSearchFor="\r\nDate: ";
startpos=m_sHeader.Find(sSearchFor);
if (startpos<0) return FALSE;
endpos = m_sHeader.Mid(startpos+sSearchFor.GetLength()).Find("\r\n");
//DATE=m_sHeader.Mid(startpos+sSearchFor.GetLength(),endpos));
//This is incorrect ! We have to parse the Date: line !!!
//Anyone likes to write a parser for the different formats a date string may have ?
m_tDateTime = m_tDateTime.GetCurrentTime();
sSearchFor="\r\nSubject: ";
startpos=m_sHeader.Find(sSearchFor);
if (startpos<0) return FALSE;
endpos=m_sHeader.Mid(startpos+sSearchFor.GetLength()).Find("\r\n");
m_sSubject=m_sHeader.Mid(startpos+sSearchFor.GetLength(),endpos);
//ATTENTION: Cc parsing won't work, if Cc is split up in multiple lines
// Cc: recipient1 ,
// recipient2 ,
// recipient3
// won't work !!!
sSearchFor="\r\nCc: ";
startpos=m_sHeader.Find(sSearchFor);
if (startpos>=0) //no error if there's no Cc
{
endpos=m_sHeader.Mid(startpos+sSearchFor.GetLength()).Find("\r\n");
AddMultipleRecipients(m_sHeader.Mid(startpos+sSearchFor.GetLength(),endpos));
}
return TRUE;
}
void CMailMessage::DecodeBody()
{
CString sCooked = "";
LPTSTR szBad = "\r\n..\r\n";
LPTSTR szGood = "\r\n.\r\n";
int nPos;
int nBadLength = strlen( szBad );
if( m_sBody.Left( 4 ) == "..\r\n" )
m_sBody = m_sBody.Mid(1);
while( (nPos = m_sBody.Find( szBad )) > -1 )
{
sCooked = m_sBody.Mid( 0, nPos );
sCooked += szGood;
m_sBody = sCooked + m_sBody.Right( m_sBody.GetLength() - (nPos + nBadLength) );
}
}