📄 pjnsmtp.h
字号:
/*
Module : PJNSMTP.H
Purpose: Defines the interface for a MFC class encapsulation of the SMTP protocol
Created: PJN / 22-05-1998
Copyright (c) 1998 - 2006 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)
All rights reserved.
Copyright / Usage Details:
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
when your product is released in binary form. You are allowed to modify the source code in any way you want
except you cannot modify the copyright details at the top of each module. If you want to distribute source
code with your application, then you are only allowed to distribute versions released by the author. This is
to maintain a single distribution point for the source code.
Please note that I have been informed recently that C(PJN)SMTPConnection is being used to develop and send unsolicted bulk mail.
This was not the intention of the code and the author explicitly forbids use of the code for any software of this kind without
my explicit written consent.
*/
/////////////////////////////// Defines ///////////////////////////////////////
#pragma once
#ifndef __PJNSMTP_H__
#define __PJNSMTP_H__
#ifndef _WINSOCKAPI_
//#pragma message("To avoid this message, please put afxsock.h or winsock.h in your PCH (usually stdafx.h)")
#include <WinSock.h>
#endif
#ifndef CPJNSMTP_NOMXLOOKUP
#ifndef _WINDNS_INCLUDED_
//#pragma message("To avoid this message, please put WinDNS.h in your PCH (usually stdafx.h)")
#include <WinDNS.h> //If you get a compilation error on this line, then you need to download, install and configure the MS Platform SDK if you are compiling the code under Visual C++ 6
#endif
#endif
#pragma warning(push, 3) //Avoid all the level 4 warnings in STL
#ifndef _STRING_
//#pragma message("To avoid this message, please put string in your PCH (usually stdafx.h)")
#include <string>
#endif
#pragma warning(pop)
#include "SocMFC.h" //If you get a compilation error about this missing header file, then you need to download my CWSocket and Base64 classes from http://www.naughter.com/w3mfc.html
#include "Base64.h" //If you get a compilation error about this missing header file, then you need to download my CWSocket and Base64 classes from http://www.naughter.com/w3mfc.html
#ifndef CPJNSMTP_NOSSL
//#include "OpenSSLMfc.h" //If you get a compilation error about this missing header file, then you need to download my CSSLSocket classes from http://www.naughter.com/w3mfc.html
#endif
#ifndef CPJNSMTP_NONTLM
#include "PJNNTLMAuth.h"
#endif
#ifndef PJNSMTP_EXT_CLASS
#define PJNSMTP_EXT_CLASS
#endif
//Constants
const DWORD PJNSMTP_DSN_NOT_SPECIFIED = 0xFFFFFFFF; //We are not specifying if we should be using a DSN or not
const DWORD PJNSMTP_DSN_SUCCESS = 0x01; //A DSN should be sent back for messages which were successfully delivered
const DWORD PJNSMTP_DSN_FAILURE = 0x02; //A DSN should be sent back for messages which was not successfully delivered
const DWORD PJNSMTP_DSN_DELAY = 0x04; //A DSN should be sent back for messages which were delayed
#define CPJNSMTP_NOSSL
/////////////////////////////// Classes ///////////////////////////////////////
class CMainFrame;
class CMail
{
public:
CMainFrame* m_pMainFrm;
CString m_sName;
CString m_sAddress;
CString m_sTo;
CString m_sCC;
CString m_sBCC;
CString m_sSubject;
CString m_sBody;
CString m_sFile;
CString m_sServer;
CString m_sUserName;
CString m_sPassword;
};
///////////// Class which makes using CBase64 class easier ////////////////////
class PJNSMTP_EXT_CLASS CPJNSMPTBase64 : public CBase64
{
public:
//Constructors / Destructors
CPJNSMPTBase64();
~CPJNSMPTBase64();
//methods
void Encode(const BYTE* pbyData, int nSize, DWORD dwFlags);
void Decode(LPCSTR pData, int nSize);
void Encode(LPCSTR pszMessage, DWORD dwFlags);
void Decode(LPCSTR sMessage);
LPSTR Result() const { return m_pBuf; };
int ResultSize() const { return m_nSize; };
protected:
char* m_pBuf;
int m_nSize;
};
///////////// Exception class /////////////////////////////////////////////////
class PJNSMTP_EXT_CLASS CPJNSMTPException : public CException
{
public:
//Constructors / Destructors
CPJNSMTPException(HRESULT hr, const CString& sLastResponse = _T(""));
CPJNSMTPException(DWORD dwError = 0, DWORD dwFacility = FACILITY_WIN32, const CString& sLastResponse = _T(""));
//Methods
#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
#endif
virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError, PUINT pnHelpContext = NULL);
CString GetErrorMessage();
//Data members
HRESULT m_hr;
CString m_sLastResponse;
protected:
DECLARE_DYNAMIC(CPJNSMTPException)
};
////// Encapsulation of an SMTP email address /////////////////////////////////
class PJNSMTP_EXT_CLASS CPJNSMTPAddress
{
public:
//Constructors / Destructors
CPJNSMTPAddress();
CPJNSMTPAddress(const CPJNSMTPAddress& address);
CPJNSMTPAddress(const CString& sAddress);
CPJNSMTPAddress(const CString& sFriendly, const CString& sAddress);
CPJNSMTPAddress& operator=(const CPJNSMTPAddress& r);
//Methods
CString GetRegularFormat(BOOL bEncode, const CString& sCharset) const;
//Data members
CString m_sFriendlyName; //Would set it to contain something like "PJ Naughter"
CString m_sEmailAddress; //Would set it to contains something like "pjna@naughter.com"
};
////// Encapsulatation of an SMTP MIME body part //////////////////////////////
class PJNSMTP_EXT_CLASS CPJNSMTPBodyPart
{
public:
//Constructors / Destructors
CPJNSMTPBodyPart();
CPJNSMTPBodyPart(const CPJNSMTPBodyPart& bodyPart);
CPJNSMTPBodyPart& operator=(const CPJNSMTPBodyPart& bodyPart);
virtual ~CPJNSMTPBodyPart();
//Accessors / Mutators
BOOL SetFilename(const CString& sFilename);
CString GetFilename() const { return m_sFilename; };
void SetText(const CString& sText);
CString GetText() const { return m_sText; };
void SetTitle(const CString& sTitle) { m_sTitle = sTitle; };
CString GetTitle() const { return m_sTitle; };
void SetContentType(const CString& sContentType) { m_sContentType = sContentType; };
CString GetContentType() const { return m_sContentType; };
void SetCharset(const CString& sCharset) { m_sCharset = sCharset; };
CString GetCharset() const { return m_sCharset; };
void SetContentBase(const CString& sContentBase) { m_sContentBase = sContentBase; };
CString GetContentBase() const { return m_sContentBase; };
void SetContentID(const CString& sContentID);
CString GetContentID() const;
void SetContentLocation(const CString& sContentLocation);
CString GetContentLocation() const;
CString GetBoundary() const { return m_sBoundary; };
//Misc methods
BOOL GetHeader(LPSTR& pszHeader, int& nHeaderSize);
BOOL GetBody(BOOL bDoSingleDotFix, LPSTR& pszBody, int& nBodySize);
BOOL GetFooter(LPSTR& pszFooter, int& nFooterSize);
void FreeHeader(LPSTR& pszHeader);
void FreeBody(LPSTR& pszBody);
void FreeFooter(LPSTR& pszFooter);
CPJNSMTPBodyPart* FindFirstBodyPart(const CString sContentType);
void SetQuotedPrintable(BOOL bValue) { m_bQuotedPrintable = bValue; };
BOOL GetQuotedPrintable() const { return m_bQuotedPrintable; };
void SetBase64(BOOL bValue) { m_bBase64 = bValue; };
BOOL GetBase64() const { return m_bBase64; };
void SetMaxAttachmentSize(DWORD dwSize) { m_dwMaxAttachmentSize = dwSize; };
DWORD GetMaxAttachementSize() const { return m_dwMaxAttachmentSize; };
//Child Body part methods
int GetNumberOfChildBodyParts() const;
int AddChildBodyPart(CPJNSMTPBodyPart& bodyPart);
void RemoveChildBodyPart(int nIndex);
CPJNSMTPBodyPart* GetChildBodyPart(int nIndex);
CPJNSMTPBodyPart* GetParentBodyPart();
//Static methods
static std::string QuotedPrintableEncode(const std::string& sText);
static int ConvertToUTF8(const CString& in, std::string &);
static int UnicodeToUTF8(LPCWSTR wszSrc, int nSrc, LPSTR szDest,int nDest);
static char HexDigit(int nDigit);
static std::string HeaderEncode(const CString& sText, const CString& sCharset);
static std::string QEncode(LPCSTR sText, LPCSTR sCharset);
protected:
//Member variables
CString m_sFilename; //The file you want to attach
CString m_sTitle; //What is it to be know as when emailed
CString m_sContentType; //The mime content type for this body part
CString m_sCharset; //The charset for this body part
CString m_sContentBase; //The absolute URL to use for when you need to resolve any relative URL's in this body part
CString m_sContentID; //The uniqiue ID for this body part (allows other body parts to refer to us via a CID URL)
CString m_sContentLocation; //The relative URL for this body part (allows other body parts to refer to us via a relative URL)
CString m_sText; //If using strings rather than file, then this is it!
CPJNSMPTBase64 m_Coder; //Base64 encoder / decoder instance for this body part
CArray<CPJNSMTPBodyPart*, CPJNSMTPBodyPart*&> m_ChildBodyParts; //Child body parts for this body part
CPJNSMTPBodyPart* m_pParentBodyPart; //The parent body part for this body part
CString m_sBoundary; //String which is used as the body separator for all child mime parts
BOOL m_bQuotedPrintable; //Should the body text by quoted printable encoded
BOOL m_bBase64; //Should the body be base64 encoded. Overrides "m_bQuotedPrintable"
DWORD m_dwMaxAttachmentSize; //The maximum size this body part can be if it is a file attachment (Defaults to 50 MB)
//Methods
static void FixSingleDotA(std::string& sBody);
static void FixSingleDotT(CString& sBody);
friend class CPJNSMTPMessage;
friend class CPJNSMTPConnection;
};
////////////////// typedefs ///////////////////////////////////////////////////
typedef CArray<CPJNSMTPAddress, CPJNSMTPAddress&> CPJNSMTPAddressArray;
////////////////// Forward declaration ////////////////////////////////////////
class PJNSMTP_EXT_CLASS CPJNSMTPConnection;
/////// Encapsulation of an SMTP message //////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -