/*
Module : SMTP.CPP
Purpose: Implementation for a MFC class encapsulation of the SMTP protocol
Created: PJN / 22-05-1998
History: PJN / 15-06-1998 1) Fixed the case where a single dot occurs on its own
in the body of a message
2) Class now supports Reply-To Header Field
3) Class now supports file attachments
PJN / 18-06-1998 1) Fixed a memory overwrite problem which was occurring
with the buffer used for encoding base64 attachments
PJN / 27-06-1998 1) The case where a line begins with a "." but contains
other text is now also catered for. See RFC821, Section 4.5.2
for further details.
2) m_sBody in CSMTPMessage has now been made protected.
Client applications now should call AddBody instead. This
ensures that FixSingleDot is only called once even if the
same message is sent a number of times.
3) Fixed a number of problems with how the MIME boundaries
were defined and sent.
4) Got rid of an unreferenced formal parameter
compiler warning when doing a release build
PJN / 11-09-1998 1) VC 5 project file is now provided
2) Attachment array which the message class contains now uses
references instead of pointers.
3) Now uses Sleep(0) to yield our time slice instead of Sleep(100),
this is the preferred way of writting polling style code in Win32
without serverly impacting performance.
4) All Trace statements now display the value as returned from
GetLastError
5) A number of extra asserts have been added
6) A AddMultipleRecipients function has been added which supports added a
number of recipients at one time from a single string
7) Extra trace statements have been added to help in debugging
PJN / 12-09-98 1) Removed a couple of unreferenced variable compiler warnings when code
was compiled with Visual C++ 6.0
2) Fixed a major bug which was causing an ASSERT when the CSMTPAttachment
destructor was being called in the InitInstance of the sample app.
This was inadvertingly introduced for the 1.2 release. The fix is to revert
fix 2) as done on 11-09-1998. This will also help to reduce the number of
attachment images kept in memory at one time.
Copyright (c) 1998 by PJ Naughter.
All rights reserved.
*/
//////////////// Includes ////////////////////////////////////////////
#include "stdafx.h"
#include "smtp.h"
//////////////// Macros / Locals /////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char CSMTPAttachment::m_base64tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz0123456789+/";
#define BASE64_MAXLINE 76
#define EOL "\r\n"
//////////////// Implementation //////////////////////////////////////
CSMTPSocket::CSMTPSocket()
{
m_hSocket = INVALID_SOCKET; //default to an invalid scoket descriptor
}
CSMTPSocket::~CSMTPSocket()
{
Close();
}
BOOL CSMTPSocket::Create()
{
m_hSocket = socket(AF_INET, SOCK_STREAM, 0);
return (m_hSocket != INVALID_SOCKET);
}
BOOL CSMTPSocket::Connect(LPCTSTR pszHostAddress, int nPort)
{
//For correct operation of the T2A macro, see MFC Tech Note 59
USES_CONVERSION;
//must have been created first
ASSERT(m_hSocket != INVALID_SOCKET);
LPSTR lpszAscii = T2A((LPTSTR)pszHostAddress);
//Determine if the address is in dotted notation
SOCKADDR_IN sockAddr;
ZeroMemory(&sockAddr, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons((u_short)nPort);
sockAddr.sin_addr.s_addr = inet_addr(lpszAscii);
//If the address is not dotted notation, then do a DNS
//lookup of it.
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
{
LPHOSTENT lphost;
lphost = gethostbyname(lpszAscii);
if (lphost != NULL)
sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
else
{
WSASetLastError(WSAEINVAL);
return FALSE;
}
}
//Call the protected version which takes an address
//in the form of a standard C style struct.
return Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}
BOOL CSMTPSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
return (connect(m_hSocket, lpSockAddr, nSockAddrLen) != SOCKET_ERROR);
}
BOOL CSMTPSocket::Send(LPCSTR pszBuf, int nBuf)
{
//must have been created first
ASSERT(m_hSocket != INVALID_SOCKET);
return (send(m_hSocket, pszBuf, nBuf, 0) != SOCKET_ERROR);
}
int CSMTPSocket::Receive(LPSTR pszBuf, int nBuf)
{
//must have been created first
ASSERT(m_hSocket != INVALID_SOCKET);
return recv(m_hSocket, pszBuf, nBuf, 0);
}
void CSMTPSocket::Close()
{
if (m_hSocket != INVALID_SOCKET)
{
VERIFY(SOCKET_ERROR != closesocket(m_hSocket));
m_hSocket = INVALID_SOCKET;
}
}
BOOL CSMTPSocket::IsReadible(BOOL& bReadible)
{
timeval timeout = {0, 0};
fd_set fds;
FD_ZERO(&fds);
FD_SET(m_hSocket, &fds);
int nStatus = select(0, &fds, NULL, NULL, &timeout);
if (nStatus == SOCKET_ERROR)
{
return FALSE;
}
else
{
bReadible = !(nStatus == 0);
return TRUE;
}
}
CSMTPAddress::CSMTPAddress(const CString& sAddress) : m_sEmailAddress(sAddress)
{
ASSERT(m_sEmailAddress.GetLength()); //An empty address is not allowed
}
CSMTPAddress::CSMTPAddress(const CString& sFriendly, const CString& sAddress) :
m_sFriendlyName(sFriendly), m_sEmailAddress(sAddress)
{
ASSERT(m_sEmailAddress.GetLength()); //An empty address is not allowed
}
CSMTPAddress& CSMTPAddress::operator=(const CSMTPAddress& r)
{
m_sFriendlyName = r.m_sFriendlyName;
m_sEmailAddress = r.m_sEmailAddress;
return *this;
}
CString CSMTPAddress::GetRegularFormat() const
{
ASSERT(m_sEmailAddress.GetLength()); //Email Address must be valid
CString sAddress;
if (m_sFriendlyName.IsEmpty())
sAddress = m_sEmailAddress; //Just transfer the address across directly
else
sAddress.Format(_T("%s <%s>"), m_sFriendlyName, m_sEmailAddress);
return sAddress;
}
CSMTPAttachment::CSMTPAttachment()
{
m_pszEncoded = NULL;
m_nEncodedSize = 0;
}
CSMTPAttachment::~CSMTPAttachment()
{
//free up any memory we allocated
if (m_pszEncoded)
{
delete [] m_pszEncoded;
m_pszEncoded = NULL;
}
}
BOOL CSMTPAttachment::Attach(const CString& sFilename)
{
ASSERT(sFilename.GetLength()); //Empty Filename !
//free up any memory we previously allocated
if (m_pszEncoded)
{
delete [] m_pszEncoded;
m_pszEncoded = NULL;
}
//determine the file size
CFileStatus fs;
if (!CFile::GetStatus(sFilename, fs))
{
TRACE(_T("Failed to get the status for file %s, probably does not exist\n"), sFilename);
return FALSE;
}
//open up the file for reading in
CFile infile;
if (!infile.Open(sFilename, CFile::modeRead | CFile::shareDenyWrite))
{
TRACE(_T("Failed to open file to be attached\n"));
return FALSE;
}
//read in the contents of the input file
char* pszIn = new char[fs.m_size];
infile.Read(pszIn, fs.m_size);
//allocate the encoded buffer
int
没有合适的资源?快使用搜索试试~ 我知道了~
smtp.zip_SMTP 类_visual c_发邮件
共16个文件
h:3个
cpp:3个
ncb:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 47 浏览量
2022-09-22
18:04:52
上传
评论
收藏 29KB ZIP 举报
温馨提示
这是个发邮件的支持stmp协议的类,特好用
资源详情
资源评论
资源推荐
收起资源包目录
smtp.zip (16个子文件)
smtp
smtpconnection.001 6KB
smtp.gif 295B
smtpconnection.dsw 553B
smtp.cpp 26KB
smtpconnection.opt 54KB
smtpconnection.ncb 49KB
StdAfx.cpp 205B
resource.h 494B
main.cpp 2KB
smtp.htm 37KB
StdAfx.h 577B
smtp.h 5KB
smtpconnection.plg 972B
smtpconnection.dsp 6KB
vcer.net.url 71B
www.pudn.com.txt 218B
共 16 条
- 1
寒泊
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0