如何使用Indy向存储在TListView组件中的多个电子邮件地址发送邮件?

63lcw9qa  于 2022-10-16  发布在  其他
关注(0)|答案(1)|浏览(139)

我正在尝试从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;
nhn9ugyo

nhn9ugyo1#

TIdMessage.Recipients.EMailAddresses被设计为包含多个电子邮件地址的单个分隔字符串,但您在循环中反复为其分配单个电子邮件地址,因此它将只包含您分配的最后一个电子邮件地址。
在这种情况下,请使用TIdMessage.Recipients.Add()而不是TIdMessage.Recipients.EMailAddresses
此外,您没有正确地枚举TListView项。如果引发异常,您并不是在保护代码不会泄漏内存。
试着这样做:

var
  smtpServer: TIdSMTP;
  msgEmail: TIdMessage;
  ssl: TIdSSLIOHandlerSocketOpenSSL;
  i: integer;
begin
  smtpServer := TIdSMTP.Create(nil);
  try
    msgEmail := TIdMessage.Create(smtpServer);

    msgEmail.Sender.Address := edtSenderAddress.Text;
    msgEmail.Sender.Name := edtSenderName.Text;
    msgEmail.ReplyTo.EMailAddresses := edtReplyTo.Text;

    for i := 0 to lsvRecipients.Count-1 do
    begin
      msgEmail.Recipients.Add.Address := lsvRecipients.Items[i].Caption;
      // or:
      // msgEmail.Recipients.Add.Text := lsvRecipients.Items[i].Caption;
    end;

    msgEmail.Subject := edtSubject.Text;
    msgEmail.Body.Text := redBody.Text;

    ssl := TIdSSLIOHandlerSocketOpenSSL.Create(smtpServer);
    ssl.SSLOptions.Method := sslvTLSv1_2;
    ssl.SSLOptions.Mode := sslmUnassigned;
    ssl.SSLOptions.VerifyMode := [];
    ssl.SSLOptions.VerifyDepth := 0;

    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;
    try
      smtpServer.Send(msgEmail);
    finally
      smtpServer.Disconnect;
    end;
  finally
    smtpServer.Free;
  end;

  // Output
  Beep;
  ShowMessage('Email Sent!');
end;

procedure TfrmEmailBomber.btnAddEmailClick(Sender: TObject);
var
  emailToAdd: TListItem;
begin
  emailToAdd := lsvRecipients.Items.Add;
  emailToAdd.Caption := edtAddEmail.Text;
end;

相关问题