// MailMessage.cpp: implementation of the CMailMessage class.
// Copyright (c) 1998, Wes Clyburn
//
// Modified to have Header and Body handling in this class rather than in any
// class that uses instances of CMailMessage.
// Copyright (c) 1998 Michael Krebs
//////////////////////////////////////////////////////////////////////
#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;
}
// sEmailAddress and sFriendlyName are OUTPUT parameters.
// If the function fails, it will return FALSE, and the OUTPUT
// parameters will not be touched.
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 );
// Add Recipients
//
length = strlen( szRecipients );
buf = new TCHAR[ length + 1 ]; // Allocate a work area (don't touch parameter itself)
strcpy( buf, szRecipients );
for( pos = 0, start = 0; pos <= length; pos++ )
{
if( buf[ pos ] == ';' ||
buf[ pos ] == 0 )
{
// First, pick apart the sub-strings (separated by ';')
// Store it in sTemp.
//
buf[ pos ] = 0; // Redundant when at the end of string, but who cares.
sTemp = &buf[ start ];
// Now divide the substring into friendly names and e-mail addresses.
//
nMark = sTemp.Find( '<' );
if( nMark >= 0 )
{
sFriendly = sTemp.Left( nMark );
nMark2 = sTemp.Find( '>' );
if( nMark2 < nMark )
{
delete[] buf;
return FALSE;
}
// End of mark at closing bracket or end of string
nMark2 > -1 ? nMark2 = nMark2 : nMark2 = sTemp.GetLength() - 1;
sEmail = sTemp.Mid( nMark + 1, nMark2 - (nMark + 1) );
}
else
{
sEmail = sTemp;
sFriendly = "";
}
AddRecipient( sEmail, sFriendly );
start = pos + 1;
}
}
delete[] buf;
return TRUE;
}
BOOL CMailMessage::EncodeHeader()
{
CString sTo;
CString sDate;
if( GetNumRecipients() <= 0 )
return FALSE;
m_sHeader = ""; // Clear it
// Get the recipients into a single string
sTo = "";
CString sEmail = "";
CString sFriendly = "";
for( int i = 0; i < GetNumRecipients(); i++ )
{
GetRecipient( sEmail, sFriendly, i );
sTo += ( i > 0 ? "," : "" );
sTo += sFriendly;
sTo += "<";
sTo += sEmail;
sTo += ">";
}
m_tDateTime = m_tDateTime.GetCurrentTime();
// Format: Mon, 01 Jun 98 01:10:30 GMT
sDate = m_tDateTime.Format( "%a, %d %b %y %H:%M:%S %Z" );
m_sHeader.Format( "Date: %s\r\n"\
"From: %s\r\n"\
"To: %s\r\n"\
"Subject: %s\r\n",
// Include other extension lines if desired
(LPCTSTR)sDate,
(LPCTSTR)m_sFrom,
(LPCTSTR)sTo,
(LPCTSTR)m_sSubject);
return TRUE;
}
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 <rec1@ab.cd>,
// recipient2 <rec2@ab.cd>,
// recipient3 <rec3@ab.cd>
// 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::EncodeBody()
{
CString sCooked = "";
LPTSTR szBad = "\r\n.\r\n";
LPTSTR szGood = "\r\n..\r\n";
int nPos;
int nBadLength = strlen( szBad );
if( m_sBody.Left( 3 ) == ".\r\n" )
m_sBody = "." + m_sBody;
//
// This is a little inefficient because it beings a search
// at the beginning of the string each time. This was
// the only thing I could think of that handled ALL variations.
// In particular, the sequence "\r\n.\r\n.\r\n" is troublesome.
// (Even CStringEx's FindReplace wouldn't handle that situation
// with the global flag set.)
//
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) );
}
}
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);
//
// This is a little inefficient because it beings a search
// at the beginning of the string each time. This was
// the only thing I could think of that handled ALL variations.
// In particular, the sequence "\r\n.\r\n.\r\n" is troublesome.
// (Even CStringEx's FindReplace wouldn't handle that situation
// with the global flag set.)
//
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) );
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Email客户端源代码程序共大家学习
共38个文件
h:7个
sbr:6个
cpp:6个
4星 · 超过85%的资源 需积分: 10 23 下载量 167 浏览量
2011-06-08
07:07:03
上传
评论
收藏 735KB RAR 举报
温馨提示
Email客户端源代码程序共大家学习Email客户端源代码程序共大家学习Email客户端源代码程序共大家学习Email客户端源代码程序共大家学习
资源推荐
资源详情
资源评论
收起资源包目录
Mail.tar (38个子文件)
Mail
SMTP.h 2KB
StdAfx.cpp 208B
Mail.rc 6KB
resource.h 1KB
SMTP.cpp 6KB
POP3.cpp 5KB
Mail.plg 970B
MailDlg.cpp 6KB
MailMessage.h 2KB
Mail.ncb 129KB
Debug
vc60.pdb 364KB
MailDlg.sbr 0B
POP3.sbr 0B
POP3.obj 32KB
MailDlg.obj 34KB
MailMessage.obj 56KB
MailMessage.sbr 0B
Mail.obj 15KB
Mail.exe 136KB
SMTP.obj 30KB
StdAfx.obj 104KB
StdAfx.sbr 1.33MB
Mail.pdb 465KB
Mail.res 3KB
Mail.sbr 0B
SMTP.sbr 0B
Mail.h 1KB
Mail.cpp 2KB
StdAfx.h 1KB
Mail.dsp 4KB
MailDlg.h 2KB
Mail.dsw 559B
MailMessage.cpp 7KB
res
Mail.ico 1KB
Mail.rc2 420B
Mail.opt 53KB
Mail.aps 21KB
POP3.h 2KB
共 38 条
- 1
资源评论
- zhouq57993879102014-03-17不是我想要的,不过还是很好了
- zhouben252015-09-12可惜不是Android的
- z8090113102014-08-27东西是不错,可惜我不能完全用.
- lxs19912012-12-13研究代码,课程设计之需,谢啦
- 心灵密码12014-06-12一般的资源,对我没有什么用
nethack2017
- 粉丝: 26
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功