smtp协议 VC实现发送邮件

本文档展示了如何使用VC2005实现SMTP协议发送邮件。通过设置SMTP服务器、账号、密码、发件人、收件人、主题和日期等信息,并添加邮件正文和附件,实现邮件的发送。主要涉及Winsock2库、Base64编码以及SMTP相关命令的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于工作需要,查阅了相关资料,利用VC2005实现了邮件发送,源程序如下:

//-------------------------------------------------------SmtpSendEmail.h------------------------------------

#pragma once
#include <list>
#include <string>
#include <fstream>
#include<WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;

#ifndef _SMTPSENDEMAIL_H
#define _SMTPSENDEMAIL_H

//发送线程
DWORD WINAPI SendMailThread(LPVOID lpParam);

class SmtpSendEmail
{
public:
SmtpSendEmail(void);
virtual ~SmtpSendEmail(void);

public:
void SetPost( int nPost );
void SetHost( string strHost );
void SetAccount( string strAccount );
void SetPassword( string strPassword );
void SetMailFrom( string strMailFrom );
void SetSendTo( string strSendTo );
void SetSubject( string strSubject );
void SetDateTime( string strDateTime );
bool AddDataFromString( string strData );
bool AddDataFromBuffer( char* szData, int iLen );
bool AddDataFromFile( string strFileName );
bool AddAttachedFile( string strFilePath, string strFileName );
bool SandThread();
bool StarMailThread();

private:
int Base64EncodeLen( int nSrcLen );
bool Base64Encode( const BYTE* szSrcData, int nSrcLen, BYTE* szDestData, int* pnDestLen );
bool CheckResponse(int nResCode);
bool ConnectServ();
bool SendHelo();
bool SendEhlo();
bool AutoLogin();
bool EmailFrom();
bool EmailTo();
bool DataServ();
bool SendData();
bool SendAttachedFile();
bool QuitServ();

private:
    static const char   m_szBase64CodeTable[];
    static const string MIMEMultipartMixedLogin;
    static const string MIMETextPlainLogin;
    static const string MyBoundary;
    static const string CTCodeQP;
    static const string CTCodeBase64;
    static const string CTTextPlainCharCodeGB2312;
    static const string CTAppOctetStreamName;
    static const string CDAttachemntFileName;

    struct SMTPSTRU
    {
        int    nPost;
        string strHost;
        string strAccount;
        string strPassword;
        string strMailFrom;
        string strSendTo;
        string strSubject;
        string strDateTime;
        string strData;
    };
    struct FILEINFOSTRU
    {
        string strPath;
        string strFile;
    };

SOCKET             m_hSocket;
    SMTPSTRU           m_smtpPro;
    list<FILEINFOSTRU> m_listFileInfo;
};

#endif


//------------------------------------------------SmtpSendEmail.cpp------------------------------------------------------

#include "StdAfx.h"
#include "SmtpSendEmail.h"

//发送线程
DWORD WINAPI SendMailThread(LPVOID lpParam)
{
((SmtpSendEmail*)lpParam)->SandThread();
return 0;
}

const char SmtpSendEmail::m_szBase64CodeTable[]       = { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" };
const string SmtpSendEmail::MIMEMultipartMixedLogin   = "X-Mailer: Gddsky mailer/r/n MIME-Version: 1.0/r/nContent-type: multipart/mixed;boundary=/"===_000_Gddsky_000_===/"/r/n/r/n";
const string SmtpSendEmail::MIMETextPlainLogin        = "X-Mailer: Gddsky mailer/r/nMIME-Version: 1.0/r/nContent-type: text/plain;charset=/"GB2312/"/r/n Content-Transfer-Encoding: base64/r/n/r/n";
const string SmtpSendEmail::MyBoundary                = "/r/n--===_000_Gddsky_000_===/r/n";
const string SmtpSendEmail::CTCodeQP                  = "Content-Transfer-Encoding: quoted-printable/r/n/r/n";
const string SmtpSendEmail::CTCodeBase64    &nb

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值