gpt4 book ai didi

c++ - 使用 Dev C++ 将邮件发送到需要身份验证的 smtp 服务器

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:26 25 4
gpt4 key购买 nike

我正在开发一个连接到 tcp 服务器的程序,获取一些数据并在某些情况下需要通过电子邮件发送警报。(我正在帮助某人完成学校项目)我正在使用 DevC++。

我已经好几年没接触过编程了,也从来没有在网络环境下做过任何编程。 (希望有意义)

我让 TCP 客户端和日志文件部分运行,但我无法让邮件发送部分工作。

由于我相对缺乏经验,我已经浪费了很多时间,例如,首先我想到尝试 POCO,但现在看来您需要 Visual C++ 来构建库。接下来我尝试了 jwsmtp,但是我能找到的示例没有进行身份验证,现在看来身份验证是必须的。接下来我尝试了 libCurl,但无法让示例工作,首先我得到 CURLOPT_MAIL_FROM 未在此范围内声明,我在一些帖子中读到由最新版本的错误引起的,然后 curl 头文件开始提供所有各种错误。

我的问题是我现在快没时间了。我很想自己完成所有工作,甚至学到足够的知识来编写自己的代码,而不仅仅是修改和粘贴示例,但我做出了 promise ,但截止日期并没有给我这个选择。

有人可以帮助解决任何实际上可以在 Windows 上使用 DevC++ 向 gmail 帐户发送邮件的问题吗?

最佳答案

我假设您将使用 Mingw 和 MSYS。我所做的是从以下位置下载最新的 OpenSSL:http://www.openssl.org/source/

然后我打开 MSYS 并运行以下命令:

./configure mingw no-shared --prefix='C:/OpenSSL'

如果成功,它应该打印Configured for mingw

接下来我转到 OpenSSL 源代码,然后是测试文件夹。我打开了 md2test.c 并将 dummytest.c 替换为:#include "dummytest.c"。我对 rc5test.cjpaketest.c 做了同样的事情。

接下来我运行了以下命令:

make depend && make install

这将构建静态库。如果要构建共享库,则需要在第一个命令(./configure 行)中将 no-shared 替换为 shared .

完成后,我创建了一些原始套接字并按如下方式发送电子邮件(需要在 sendemail 函数中进行一些错误检查,但我希望你能理解它的要点。它有效):

/**  © 2014, Brandon T. All Rights Reserved.
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
*/

#if defined _WIN32 || defined _WIN64
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#endif

#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#ifndef __cplusplus
typedef enum {false, true} bool;
#endif

bool ssl_init_count = 0;

typedef struct
{
int sock;
const char* address;
unsigned short port;
} sock_info;

typedef struct
{
SSL* ssl;
SSL_CTX* ctx;
} ssl_info;

typedef struct
{
char b64username[256];
char b64password[256];
} email;

bool initsocket()
{
#if defined _WIN32 || defined _WIN64
WSADATA wsaData = {0};
return !WSAStartup(MAKEWORD(2, 2), &wsaData);
#else
return true;
#endif
}

void destroysocket(sock_info* info)
{
#if defined _WIN32 || defined _WIN64
shutdown(info->sock, SD_BOTH);
closesocket(info->sock);
WSACleanup();
#else
shutdown(info->sock, SHUT_RDWR);
close(info->sock);
#endif
}

bool connectsocket(sock_info* info)
{
struct sockaddr_in* sockaddr_ipv4 = NULL;
struct addrinfo* it = NULL, *result = NULL;
getaddrinfo(info->address, NULL, NULL, &result);

for (it = result; it != NULL; it = it->ai_next)
{
sockaddr_ipv4 = (struct sockaddr_in*)it->ai_addr;
info->address = inet_ntoa(sockaddr_ipv4->sin_addr);

if (strncmp(info->address, "0.0.0.0", 7))
break;
}
freeaddrinfo(result);

if ((info->sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
perror("Error creating socket..");
return false;
}

struct sockaddr_in SockAddr;
memset(&SockAddr, 0, sizeof(SockAddr));
SockAddr.sin_port = htons(info->port);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = inet_addr(info->address);

if (connect(info->sock, (struct sockaddr*)&SockAddr, sizeof(SockAddr)) < 0)
{
perror("Error connecting socket..");
return false;
}

return true;
}

bool setssl(sock_info* sockinfo, ssl_info* sslinfo)
{
sslinfo->ctx = SSL_CTX_new(SSLv23_client_method());

if (sslinfo->ctx)
{
sslinfo->ssl = SSL_new(sslinfo->ctx);
SSL_set_fd(sslinfo->ssl, sockinfo->sock);
return SSL_connect(sslinfo->ssl) != -1;
}
return false;
}

void removessl(sock_info* sockinfo, ssl_info* sslinfo)
{
if (sslinfo->ctx)
{
SSL_CTX_free(sslinfo->ctx);
}

if (sslinfo->ssl)
{
SSL_shutdown(sslinfo->ssl);
SSL_free(sslinfo->ssl);
}

sslinfo->ssl = NULL;
sslinfo->ctx = NULL;
}

void initssl()
{
if (!ssl_init_count)
{
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
}

++ssl_init_count;
}

void freessl()
{
if (!--ssl_init_count)
{
ERR_free_strings();
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
}
}

void sslb64encode(const char* buffer, char* outbuffer)
{
char* b64str = NULL;
BIO* b64 = BIO_new(BIO_f_base64());
BIO* mem = BIO_new(BIO_s_mem());

BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
b64 = BIO_push(b64, mem);
BIO_write(b64, buffer, strlen(buffer));
BIO_flush(b64);

int len = BIO_get_mem_data(mem, &b64str);
memcpy(outbuffer, b64str, len);
outbuffer[len] = '\0';
BIO_free_all(b64);
}

void initemail(const char* username, const char* password, email* outemail)
{
char ubuffer[256];
char pbuffer[256];
unsigned int bytes_written = 0;

sslb64encode(username, &ubuffer[0]);
sslb64encode(password, &pbuffer[0]);

sprintf(outemail->b64username, "%s", ubuffer);
sprintf(outemail->b64password, "%s", pbuffer);
}

bool printsocketbuffer(ssl_info* sslinfo)
{
char buffer[1024];
unsigned int bytes_read = SSL_read(sslinfo->ssl, &buffer[0], sizeof(buffer));
if (bytes_read > 0)
{
printf("%.*s", bytes_read, buffer);
return true;
}
return false;
}

void sendemail(ssl_info* sslinfo, const char* username, const char* password, const char* recipient, const char* from, const char* to, const char* message, const char* subject, unsigned int messagelen)
{
email em;
char buffer[512];
unsigned int bufflen = sizeof(buffer);
initemail(username, password, &em);

SSL_write(sslinfo->ssl, "HELO\r\n", 6);
printsocketbuffer(sslinfo);

SSL_write(sslinfo->ssl, "AUTH LOGIN\r\n", 12);
printsocketbuffer(sslinfo);

bufflen = sprintf(buffer, "%s\r\n", em.b64username);
SSL_write(sslinfo->ssl, buffer, bufflen);
printsocketbuffer(sslinfo);

bufflen = sprintf(buffer, "%s\r\n", em.b64password);
SSL_write(sslinfo->ssl, buffer, bufflen);
printsocketbuffer(sslinfo);
printsocketbuffer(sslinfo);

bufflen = sprintf(buffer, "MAIL FROM: <%s>\r\n", username);
SSL_write(sslinfo->ssl, buffer, bufflen);
printsocketbuffer(sslinfo);

bufflen = sprintf(buffer, "RCPT TO: <%s>\r\n", recipient);
SSL_write(sslinfo->ssl, buffer, bufflen);
printsocketbuffer(sslinfo);

SSL_write(sslinfo->ssl, "DATA\r\n", 6);
printsocketbuffer(sslinfo);

bufflen = sprintf(buffer, "From: <%s><%s>\r\n", from, username);
bufflen += sprintf(&buffer[bufflen], "To: <%s><%s>\r\n", to, recipient);
bufflen += sprintf(&buffer[bufflen], "Subject: <%s>\r\n", subject);
SSL_write(sslinfo->ssl, buffer, bufflen);

bufflen = 0;
while (bufflen < messagelen)
{
bufflen += SSL_write(sslinfo->ssl, &message[bufflen], messagelen - bufflen);
}

SSL_write(sslinfo->ssl, "\r\n.\r\n", 5);
printsocketbuffer(sslinfo);

SSL_write(sslinfo->ssl, "QUIT\r\n", 6);
printsocketbuffer(sslinfo);
}

int main()
{
ssl_info sslinfo = {0};
sock_info sockinfo = {0, "smtp.gmail.com", 465};

const char* username = "ICantChooseUsernames@gmail.com";
const char* password = "*****";
const char* recipient = "ICantChooseUsernames@gmail.com";
const char* message = "hello there!";
const char* subject = "Testing Emails";
const char* from = "Brandon";
const char* to = "Brandon";
unsigned int messagelen = strlen(message);


if (initsocket())
{
if (connectsocket(&sockinfo))
{
initssl();

if (setssl(&sockinfo, &sslinfo))
{
sendemail(&sslinfo, username, password, recipient, from, to, message, subject, messagelen);
}
else
{
ERR_print_errors_fp(stderr);
}

removessl(&sockinfo, &sslinfo);
freessl();
}

destroysocket(&sockinfo);
}

return 0;
}

关于c++ - 使用 Dev C++ 将邮件发送到需要身份验证的 smtp 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23664181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com