c++使用smtp发送邮件
时间: 2023-08-23 12:06:50 浏览: 107
发送邮件需要使用SMTP协议,可以使用C++编写SMTP客户端实现邮件发送功能。下面是一个使用C++实现SMTP发送邮件的示例代码:
```c++
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
int main()
{
// 初始化Winsock库
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
cout << "Failed to initialize Winsock library." << endl;
return 1;
}
// 创建套接字
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{
cout << "Failed to create socket." << endl;
WSACleanup();
return 1;
}
// 获取SMTP服务器的IP地址
struct addrinfo* result = NULL, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo("smtp.example.com", "25", &hints, &result) != 0)
{
cout << "Failed to get SMTP server IP address." << endl;
closesocket(sock);
WSACleanup();
return 1;
}
// 连接SMTP服务器
if (connect(sock, result->ai_addr, (int)result->ai_addrlen) != 0)
{
cout << "Failed to connect to SMTP server." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的欢迎信息
char recvbuf[1024];
int recvbuflen = 1024;
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive welcome message from SMTP server." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送EHLO命令
const char* ehlo = "EHLO example.com\r\n";
if (send(sock, ehlo, (int)strlen(ehlo), 0) <= 0)
{
cout << "Failed to send EHLO command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to EHLO command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送AUTH LOGIN命令
const char* auth = "AUTH LOGIN\r\n";
if (send(sock, auth, (int)strlen(auth), 0) <= 0)
{
cout << "Failed to send AUTH LOGIN command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to AUTH LOGIN command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送用户名
const char* username = "your_email@example.com";
int len = (int)strlen(username);
for (int i = 0; i < len; i++)
{
username[i] = base64_encode(username[i]);
}
const char* user = username;
if (send(sock, user, (int)strlen(user), 0) <= 0)
{
cout << "Failed to send username." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
if (send(sock, "\r\n", 2, 0) <= 0)
{
cout << "Failed to send CRLF." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to username." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送密码
const char* password = "your_password";
len = (int)strlen(password);
for (int i = 0; i < len; i++)
{
password[i] = base64_encode(password[i]);
}
const char* pass = password;
if (send(sock, pass, (int)strlen(pass), 0) <= 0)
{
cout << "Failed to send password." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
if (send(sock, "\r\n", 2, 0) <= 0)
{
cout << "Failed to send CRLF." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to password." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送MAIL FROM命令
const char* mailfrom = "MAIL FROM:<your_email@example.com>\r\n";
if (send(sock, mailfrom, (int)strlen(mailfrom), 0) <= 0)
{
cout << "Failed to send MAIL FROM command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to MAIL FROM command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送RCPT TO命令
const char* rcptto = "RCPT TO:<recipient_email@example.com>\r\n";
if (send(sock, rcptto, (int)strlen(rcptto), 0) <= 0)
{
cout << "Failed to send RCPT TO command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to RCPT TO command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送DATA命令
const char* data = "DATA\r\n";
if (send(sock, data, (int)strlen(data), 0) <= 0)
{
cout << "Failed to send DATA command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to DATA command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送邮件内容
const char* content = "From: your_email@example.com\r\n"
"To: recipient_email@example.com\r\n"
"Subject: Test\r\n"
"\r\n"
"This is a test email.\r\n"
".\r\n";
if (send(sock, content, (int)strlen(content), 0) <= 0)
{
cout << "Failed to send email content." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to email content." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 发送QUIT命令
const char* quit = "QUIT\r\n";
if (send(sock, quit, (int)strlen(quit), 0) <= 0)
{
cout << "Failed to send QUIT command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器的响应
if (recv(sock, recvbuf, recvbuflen, 0) <= 0)
{
cout << "Failed to receive response to QUIT command." << endl;
freeaddrinfo(result);
closesocket(sock);
WSACleanup();
return 1;
}
cout << recvbuf << endl;
// 关闭套接字
freeaddrinfo(result);
closesocket(sock);
// 关闭Winsock库
WSACleanup();
return 0;
}
// Base64编码
char base64_encode(char c)
{
if (c >= 'A' && c <= 'Z')
{
return c - 'A';
}
else if (c >= 'a' && c <= 'z')
{
return c - 'a' + 26;
}
else if (c >= '0' && c <= '9')
{
return c - '0' + 52;
}
else if (c == '+')
{
return 62;
}
else if (c == '/')
{
return 63;
}
else
{
return -1;
}
}
```
上面的代码使用了Windows Sockets API来实现SMTP客户端,需要在Windows系统下编译运行。在代码中,需要将"smtp.example.com"和"your_email@example.com"替换为实际的SMTP服务器地址和发件人邮箱地址。另外,需要在代码中指定收件人邮箱地址、邮件主题和邮件内容。
阅读全文