匿名用户
1级
2008-04-07 回答
#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()
{
}
//将收件人地址加到m_Recipients数组中
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;
}
//从类CRecipient中提取收信人地址
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();
}
//收信人地址->数组m_Recipients
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( '= 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::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( "" );
}
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;
}