我正在尝试从TListView
中获取电子邮件列表,并将它们分配给TIdMessage.Recipients.EMailAddresses
。我正在尝试获取ListView标题,并将它们分配为收件人电子邮件地址,但我想不出如何操作。
以下是我目前的破解代码:
procedure TfrmEmailer.btnSendClick(Sender: TObject);
var
smtpServer: TIdSMTP;
msgEmail: TIdMessage;
ssl: TIdSSLIOHandlerSocketOpenSSL;
i: integer;
begin
smtpServer := TIdSMTP.Create(nil);
msgEmail := TIdMessage.Create(nil);
ssl := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
ssl.SSLOptions.Method := sslvTLSv1_2;
ssl.SSLOptions.Mode := sslmUnassigned;
ssl.SSLOptions.VerifyMode := [];
ssl.SSLOptions.VerifyDepth := 0;
msgEmail.Sender.Address := edtSenderAddress.Text;
msgEmail.Sender.Name := edtSenderName.Text;
msgEmail.ReplyTo.EMailAddresses := edtReplyTo.Text;
for i := 0 to length(lsvRecipients.ItemIndex) do
begin
msgEmail.Recipients.EMailAddresses := lsvRecipients.Items[0].Caption;
end;
msgEmail.Subject := edtSubject.Text;
msgEmail.Body.Text := redBody.Text;
smtpServer.IOHandler := ssl;
smtpServer.Host := edtHost.Text;
smtpServer.Port := StrToInt(edtPort.Text);
smtpServer.Username := edtUsername.Text;
smtpServer.Password := edtPassword.Text;
smtpServer.UseTLS := utUseExplicitTLS;
smtpServer.Connect;
smtpServer.Send(msgEmail);
smtpServer.Disconnect;
smtpServer.Free;
msgEmail.Free;
ssl.Free;
// Output
beep;
ShowMessage('Email Sent!');
end;
procedure TfrmEmailer.btnAddEmailClick(Sender: TObject);
begin
emailToAdd := lsvRecipients.Items.Add;
emailToAdd.Caption := edtAddEmail.Text;
end;
1条答案
按热度按时间nhn9ugyo1#
TIdMessage.Recipients.EMailAddresses
被设计为包含多个电子邮件地址的单个分隔字符串,但您在循环中反复为其分配单个电子邮件地址,因此它将只包含您分配的最后一个电子邮件地址。在这种情况下,请使用
TIdMessage.Recipients.Add()
而不是TIdMessage.Recipients.EMailAddresses
。此外,您没有正确地枚举
TListView
项。如果引发异常,您并不是在保护代码不会泄漏内存。试着这样做: