SMTP命令结构
这几天在使用SMTP工作,发现SMTP方面需要总结一些用法,网上大多数只提到了telnet用法和C ++实现方法。
以下内容参考自SMTP基本命令和用命令行发邮件
SMTP基本命令包括以下几个:HELO﹑EHLO,MAIL﹑RCPT﹑数据,AUTH登录和退出
- HELO-发件方问候收件方,后面是发件人的服务器地址或标识。收件方回答OK时标识自己的身份。问题和确认过程表明两台机器可以进行通信,同时状态参量被复位,缓冲区被清空。
- EHLO-申明身份,表示自己身份需要验证,注意这部分需要通过Telnet验证一下,是user@example.com还是用户,否则会出错。
- MAIL-这个命令用来开始传送邮件,它的后面跟随发件,它的后面跟随发件,它们的后面跟随发件,邮件地址(返回邮件地址)它们也用来当邮件无法送达时,发送失败通知。为保证邮件的成功发送,发件方的地址应该是被对方或中间转发方同意接受的。这个命令会清空有关的缓冲区,为新的邮件做准备。
- RCPT - 这个命令告诉收件方收件人的邮箱。当有多个收件人时,需要多次使用该命令RCPT TO,each times只能指明一个人。如果接收方服务器不同意转发这个地址的邮件,它必须报550错误代码通知发件,如果服务器同意转发,它要更改邮件发送路径,把最开始的目的地(换成下一个服务器)。
- DATA-收件方把该命令之后的数据作为发送的数据。数据被加入数据缓冲区中,以单独一行是“。”的行结束数据。结束行对于接收方同时意味立即开始缓冲区内的数据如果传送接收,接收方回复OK。
- QUIT-SMTP要求接收放必须回答OK,然后中断传输;在收到这个命令并回答OK前,收件方不得中断连接,即使传输出现错误。发件人在发出这个命令并收到OK答复前,也不得中断连接。
- AUTH LOGIN-登录邮箱,这一部分一般要用base64加密。
C ++实现发送邮件----感谢先辈大佬的无私奉献
内容参考自邮件正文及其附件的发送的C ++实现,并添加多个邮件接收者的功能,
需要的同学可以直接调用这个类来发送邮件,纯c++代码。(在VS2013下测试完美通过!)
废话不多说,直接上代码!
Smtp.h
#ifndef __SMTP_H__
#define __SMTP_H__
#include <list>
#include <WinSock2.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
const int MAXLEN = 1024;
const int MAX_FILE_LEN = 6000;
static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
struct FILEINFO
{
char fileName[128];
char filePath[256];
};
class CSmtp
{
public:
CSmtp(void);
CSmtp(
int port,
std::string srvDomain,
std::string userName,
std::string password,
std::string targetEmail,
std::string emailTitle,
std::string content
);
public:
~CSmtp(void);
public:
int port;
public:
std::string domain;
std::string user;
std::string pass;
std::string targetAddr;
std::string title;
std::string content;
std::list <FILEINFO *> listFile;
public:
char buff[MAXLEN + 1];
int buffLen;
SOCKET sockClient;
public:
bool CreateConn();
bool Send(std::string &message);
bool Recv();
void FormatEmailHead(std::string &email);
int Login();
bool SendEmailHead();
bool SendTextBody();
int SendAttachment_Ex();
bool SendEnd();
public:
void AddAttachment(std::string &filePath);
void DeleteAttachment(std::string &filePath);
void DeleteAllAttachment();
void SetSrvDomain(std::string &domain);
void SetUserName(std::string &user);
void SetPass(std::string &pass);
void SetTargetEmail(std::string &targetAddr);
void SetEmailTitle(std::string &title);
void SetContent(std::string &content);
void SetPort(int port);
int SendEmail_Ex();
char* base64Encode(char const* origSigned, unsigned origLength);
};
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
smtp.cpp
#include "StdAfx.h"
#include "smtp.h"
#pragma comment(lib, "ws2_32.lib") /*链接ws2_32.lib动态链接库*/
char* CSmtp::base64Encode(char const* origSigned, unsigned origLength)
{
unsigned char const* orig = (unsigned char const*)origSigned;
if (orig == NULL) return NULL;
unsigned const numOrig24BitValues = origLength / 3;
bool havePadding = origLength > numOrig24BitValues * 3;
bool havePadding2 = origLength == numOrig24BitValues * 3 + 2;
unsigned const numResultBytes = 4 * (numOrig24BitValues + havePadding);
char* result = new char[numResultBytes + 3];
unsigned i;
for (i = 0; i < numOrig24BitValues; ++i)
{
result[4 * i + 0] = base64Char[(orig[3 * i] >> 2) & 0x3F];
result[4 * i + 1] = base64Char[(((orig[3 * i] & 0x3) << 4) | (orig[3 * i + 1] >> 4)) & 0x3F];
result[4 * i + 2] = base64Char[((orig[3 * i + 1] << 2) | (orig[3 * i + 2] >> 6)) & 0x3F];
result[4 * i + 3] = base64Char[orig[3 * i + 2] & 0x3F];
}
if (havePadding)
{
result[4 * i + 0] = base64Char[(orig[3 * i] >> 2) & 0x3F];
if (havePadding2)
{
result[4 * i + 1] = base64Char[(((orig[3 * i] & 0x3) << 4) | (orig[3 * i + 1] >> 4)) & 0x3F];
result[4 * i + 2] = base64Char[(orig[3 * i + 1] << 2) & 0x3F];
}
else
{
result[4 * i + 1] = base64Char[((orig[3 * i] & 0x3) << 4) & 0x3F];
result[4 * i + 2] = '=';
}
result[4 * i + 3] = '=';
}
result[numResultBytes] = '\0';
return result;
}
CSmtp::CSmtp(void)
{
this->content = "";
this->port = 25;
this->user = "";
this->pass = "";
this->targetAddr = "";
this->title = "";
this->domain = "";
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 1);
err = WSAStartup(wVersionRequested, &wsaData);
this->sockClient = 0;
}
CSmtp::~CSmtp(void)
{
DeleteAllAttachment();
closesocket(sockClient);
WSACleanup();
}
CSmtp::CSmtp(
int port,
std::string srvDomain,
std::string userName,
std::string password,
std::string targetEmail,
std::string emailTitle,
std::string content
)
{
this->content = content;
this->port = port;
this->user = userName;
this->pass = password;
this->targetAddr = targetEmail;
this->title = emailTitle;
this->domain = srvDomain;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 1);
err = WSAStartup(wVersionRequested, &wsaData);
this->sockClient = 0;
}
bool CSmtp::CreateConn()
{
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
HOSTENT* pHostent;
pHostent = gethostbyname(domain.c_str());
addrSrv.sin_addr.S_un.S_addr = *((DWORD *)pHostent->h_addr_list[0]);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(port);
int err = connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (err != 0)
{
return false;
}
this->sockClient = sockClient;
if (false == Recv())
{
return false;
}
return true;
}
bool CSmtp::Send(std::string &message)
{
int err = send(sockClient, message.c_str(), message.length(), 0);
if (err == SOCKET_ERROR)
{
return false;
}
std::string message01;
return true;
}
bool CSmtp::Recv()
{
memset(buff, 0, sizeof(char)* (MAXLEN + 1));
int err = recv(sockClient, buff, MAXLEN, 0);
if (err == SOCKET_ERROR)
{
return false;
}
buff[err] = '\0';
return true;
}
int CSmtp::Login()
{
std::string sendBuff;
sendBuff = "EHLO ";
sendBuff += user;
sendBuff += "\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return 1;
}
sendBuff.empty();
sendBuff = "AUTH LOGIN\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return 1;
}
sendBuff.empty();
int pos = user.find('@', 0);
sendBuff = user.substr(0, pos);
char *ecode;
ecode = base64Encode(sendBuff.c_str(), strlen(sendBuff.c_str()));
sendBuff.empty();
sendBuff = ecode;
sendBuff += "\r\n";
delete[]ecode;
if (false == Send(sendBuff) || false == Recv())
{
return 1;
}
sendBuff.empty();
ecode = base64Encode(pass.c_str(), strlen(pass.c_str()));
sendBuff = ecode;
sendBuff += "\r\n";
delete[]ecode;
if (false == Send(sendBuff) || false == Recv())
{
return 1;
}
if (NULL != strstr(buff, "550"))
{
return 2;
}
if (NULL != strstr(buff, "535"))
{
return 3;
}
return 0;
}
bool CSmtp::SendEmailHead()
{
std::string sendBuff;
sendBuff = "MAIL FROM: <" + user + ">\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return false;
}
std::istringstream is(targetAddr);
std::string tmpadd;
while(is>>tmpadd)
{
sendBuff.empty();
sendBuff = "RCPT TO: <" + tmpadd + ">\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return false;
}
}
sendBuff.empty();
sendBuff = "DATA\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return false;
}
sendBuff.empty();
FormatEmailHead(sendBuff);
if (false == Send(sendBuff))
{
return false;
}
return true;
}
void CSmtp::FormatEmailHead(std::string &email)
{
email = "From: ";
email += user;
email += "\r\n";
email += "To: ";
email += targetAddr;
email += "\r\n";
email += "Subject: ";
email += title;
email += "\r\n";
email += "MIME-Version: 1.0";
email += "\r\n";
email += "Content-Type: multipart/mixed;boundary=qwertyuiop";
email += "\r\n";
email += "\r\n";
}
bool CSmtp::SendTextBody()
{
std::string sendBuff;
sendBuff = "--qwertyuiop\r\n";
sendBuff += "Content-Type: text/plain;";
sendBuff += "charset=\"gb2312\"\r\n\r\n";
sendBuff += content;
sendBuff += "\r\n\r\n";
return Send(sendBuff);
}
int CSmtp::SendAttachment_Ex()
{
for (std::list<FILEINFO *>::iterator pIter = listFile.begin(); pIter != listFile.end(); pIter++)
{
std::string sendBuff;
sendBuff = "--qwertyuiop\r\n";
sendBuff += "Content-Type: application/octet-stream;\r\n";
sendBuff += " name=\"";
sendBuff += (*pIter)->fileName;
sendBuff += "\"";
sendBuff += "\r\n";
sendBuff += "Content-Transfer-Encoding: base64\r\n";
sendBuff += "Content-Disposition: attachment;\r\n";
sendBuff += " filename=\"";
sendBuff += (*pIter)->fileName;
sendBuff += "\"";
sendBuff += "\r\n";
sendBuff += "\r\n";
Send(sendBuff);
std::ifstream ifs((*pIter)->filePath, std::ios::in | std::ios::binary);
if (false == ifs.is_open())
{
return 4;
}
char fileBuff[MAX_FILE_LEN];
char *chSendBuff;
memset(fileBuff, 0, sizeof(fileBuff));
while (ifs.read(fileBuff, MAX_FILE_LEN))
{
chSendBuff = base64Encode(fileBuff, MAX_FILE_LEN);
chSendBuff[strlen(chSendBuff)] = '\r';
chSendBuff[strlen(chSendBuff)] = '\n';
send(sockClient, chSendBuff, strlen(chSendBuff), 0);
delete[]chSendBuff;
}
chSendBuff = base64Encode(fileBuff, ifs.gcount());
chSendBuff[strlen(chSendBuff)] = '\r';
chSendBuff[strlen(chSendBuff)] = '\n';
int err = send(sockClient, chSendBuff, strlen(chSendBuff), 0);
if (err != strlen(chSendBuff))
{
return 1;
}
delete[]chSendBuff;
}
return 0;
}
bool CSmtp::SendEnd()
{
std::string sendBuff;
sendBuff = "--qwertyuiop--";
sendBuff += "\r\n.\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return false;
}
sendBuff.empty();
sendBuff = "QUIT\r\n";
return (Send(sendBuff) && Recv());
}
int CSmtp::SendEmail_Ex()
{
if (false == CreateConn())
{
return 1;
}
int err = Login();
if (err != 0)
{
return err;
}
if (false == SendEmailHead())
{
return 1;
}
if (false == SendTextBody())
{
return 1;
}
err = SendAttachment_Ex();
if (err != 0)
{
return err;
}
if (false == SendEnd())
{
return 1;
}
return 0;
}
void CSmtp::AddAttachment(std::string &filePath)
{
FILEINFO *pFile = new FILEINFO;
strcpy_s(pFile->filePath, filePath.c_str());
const char *p = filePath.c_str();
strcpy_s(pFile->fileName, p + filePath.find_last_of("\\") + 1);
listFile.push_back(pFile);
}
void CSmtp::DeleteAttachment(std::string &filePath)
{
std::list<FILEINFO *>::iterator pIter;
for (pIter = listFile.begin(); pIter != listFile.end(); pIter++)
{
if (strcmp((*pIter)->filePath, filePath.c_str()) == 0)
{
FILEINFO *p = *pIter;
listFile.remove(*pIter);
delete p;
break;
}
}
}
void CSmtp::DeleteAllAttachment()
{
for (std::list<FILEINFO *>::iterator pIter = listFile.begin(); pIter != listFile.end();)
{
FILEINFO *p = *pIter;
pIter = listFile.erase(pIter);
delete p;
}
}
void CSmtp::SetSrvDomain(std::string &domain)
{
this->domain = domain;
}
void CSmtp::SetUserName(std::string &user)
{
this->user = user;
}
void CSmtp::SetPass(std::string &pass)
{
this->pass = pass;
}
void CSmtp::SetTargetEmail(std::string &targetAddr)
{
this->targetAddr = targetAddr;
}
void CSmtp::SetEmailTitle(std::string &title)
{
this->title = title;
}
void CSmtp::SetContent(std::string &content)
{
this->content = content;
}
void CSmtp::SetPort(int port)
{
this->port = port;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
测试程序
#include "Smtp.h"
#include <iostream>
using namespace std;
int main()
{
CSmtp smtp(
25,
"smtp.163.com",
"yourname@163.com",
"yourpasswd",
"target1@163.com target2@163.com",
"消息",
"消息提示"
);
/**
//添加附件时注意,\一定要写成\\,因为转义字符的缘故
string filePath("D:\\课程设计报告.doc");
smtp.AddAttachment(filePath);
*/
int err;
if ((err = smtp.SendEmail_Ex()) != 0)
{
if (err == 1)
cout << "错误1: 由于网络不畅通,发送失败!" << endl;
if (err == 2)
cout << "错误2: 用户名错误,请核对!" << endl;
if (err == 3)
cout << "错误3: 用户密码错误,请核对!" << endl;
if (err == 4)
cout << "错误4: 请检查附件目录是否正确,以及文件是否存在!" << endl;
}
system("pause");
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
以上