我在 RAD Studio C++Builder 中更新了我的 Indy 组件,因为我想尝试将 OAUTH2 添加到我的 SMTP 电子邮件发送中。
我在 this thread(添加对 XOAUTH2 SASL 身份验证的支持)中看到 OAUTH2 的代码已推送到 GitHub。
我从 https://github.com/IndySockets/ 下载了存储库,并按照 Wiki 说明从我的 RAD Studio 12.2 安装中删除了现有的 Indy 组件。然后我构建并安装了 Indy 10.6.3.6。
我从 2024 年 3 月开始尝试了 master 分支和 sasl-oauth 分支。使用这两个分支时,我的 SMTP 不再工作并给出 TLS 握手错误。
自 RAD Studio 12 版本以来,Indy 初始化有什么变化吗?
这是我的代码,适用于原始 Indy 组件:
try
{
IdMessage = new TIdMessage;
IdMessage->Encoding = meMIME;
if (EmailBodyFormatRG->ItemIndex == 1)
{
IdMessage->ContentType = "text/html";
}
else
{
IdMessage->ContentType = "text/plain";
}
IdMessage->CharSet = "UTF-8";
IdMessage->From->Address = EmailSenderEdit->Text;
IdMessage->ReplyTo->EMailAddresses = EmailReplyToEdit->Text;
IdMessage->Recipients->EMailAddresses = EmailAddr;
IdMessage->Subject = Subject;
IdMessage->Priority = mpNormal;
IdMessage->CCList->EMailAddresses = "";
IdMessage->ReceiptRecipient->Text = "";
IdMessage->BccList->EMailAddresses = "";
IdMessage->Body->AddStrings(Body);
SMTP = new TIdSMTP;
try
{
if (EmailAuthenticateCB->Checked)
{
SSLHandler = new TIdSSLIOHandlerSocketOpenSSL(SMTP);
// SSL/TLS handshake determines the highest available SSL/TLS version dynamically
SSLHandler->SSLOptions->SSLVersions.Clear();
SSLHandler->SSLOptions->SSLVersions << sslvTLSv1 << sslvTLSv1_1 << sslvTLSv1_2;
SSLHandler->SSLOptions->Mode = sslmClient;
SSLHandler->SSLOptions->VerifyMode.Clear();
SSLHandler->SSLOptions->VerifyDepth = 0;
SMTP->IOHandler = SSLHandler;
if (EmailPortSpinEdit->Value == 587)
{
SMTP->UseTLS = utUseExplicitTLS;
}
else
{
SMTP->UseTLS = utUseImplicitTLS; // Port 465
}
}
if ((EmailUserEdit->Text != "") || (EmailPasswordEdit->Text != ""))
{
SMTP->AuthType = satSASL;
IdUserPassProvider = new TIdUserPassProvider(SMTP);
IdUserPassProvider->Username = EmailUserEdit->Text;
IdUserPassProvider->Password = EmailPasswordEdit->Text;
IdSASLCRAMSHA1 = new TIdSASLCRAMSHA1(SMTP);
IdSASLCRAMSHA1->UserPassProvider = IdUserPassProvider;
IdSASLCRAMMD5 = new TIdSASLCRAMMD5(SMTP);
IdSASLCRAMMD5->UserPassProvider = IdUserPassProvider;
IdSASLSKey = new TIdSASLSKey(SMTP);
IdSASLSKey->UserPassProvider = IdUserPassProvider;
IdSASLOTP = new TIdSASLOTP(SMTP);
IdSASLOTP->UserPassProvider = IdUserPassProvider;
IdSASLAnonymous = new TIdSASLAnonymous(SMTP);
IdSASLExternal = new TIdSASLExternal(SMTP);
IdSASLLogin = new TIdSASLLogin(SMTP);
IdSASLLogin->UserPassProvider = IdUserPassProvider;
IdSASLPlain = new TIdSASLPlain(SMTP);
IdSASLPlain->UserPassProvider = IdUserPassProvider;
TIdSASLListEntry *se = SMTP->SASLMechanisms->Add(); se->SASL = IdSASLCRAMSHA1;
TIdSASLListEntry *se1 = SMTP->SASLMechanisms->Add(); se1->SASL = IdSASLCRAMMD5;
TIdSASLListEntry *se2 = SMTP->SASLMechanisms->Add(); se2->SASL = IdSASLSKey;
TIdSASLListEntry *se3 = SMTP->SASLMechanisms->Add(); se3->SASL = IdSASLOTP;
TIdSASLListEntry *se4 = SMTP->SASLMechanisms->Add(); se4->SASL = IdSASLAnonymous;
TIdSASLListEntry *se5 = SMTP->SASLMechanisms->Add(); se5->SASL = IdSASLExternal;
TIdSASLListEntry *se6 = SMTP->SASLMechanisms->Add(); se6->SASL = IdSASLLogin;
TIdSASLListEntry *se7 = SMTP->SASLMechanisms->Add(); se7->SASL = IdSASLPlain;
SMTP->Host = EmailHostEdit->Text;
SMTP->Port = EmailPortSpinEdit->Value;
SMTP->ConnectTimeout = 30000;
SMTP->UseEhlo = true;
SMTP->Connect();
try
{
SMTP->Send(IdMessage);
}
__finally
{
SMTP->Disconnect();
}
}
else
{
SMTP->AuthType = satNone;
}
}
__finally
{
delete SMTP;
}
以前工作的 SMTP 现在提供:
Error connecting with SSL.
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
问题是 open SSL 二进制文件错误,我直接从 OPEN SSL 获取,但这对于 Indy 来说是不正确的。
Indy 目前最高支持 OpenSSL 1.0.2u,您可以从 Indy 的 OpenSSL-Binaries 存储库获取。
一旦我为我的应用程序获取了正确的 32 位 DLL,SMTP SSL 就会再次工作。 Indy OpenSSL-Binaries Repo 提供 32 位和 64 位版本。