通过XE2 delphi发送电子邮件时出错

时间:2013-07-23 15:48:52

标签: delphi email delphi-xe2

我使用这个简单的代码发送适用于大多数SMTP服务器的电子邮件,但是对于使用RFC 2554中定义的SMTP AUTH扩展的服务器。它显示了以下错误:

错误:有效的RCPT命令必须在DATA

之前

这是代码:

    SMTP.Host := 'host.com';
  SMTP.Port := 25;
  SMTP.Username:= 'user@host.com';
  SMTP.Password:= 'pass';
  MailMessages.From.Address:='address@address.com';
  MailMessages.From.Name:= 'Ehsan';
  MailMessages.Subject :=  'Test';
  MailMessages.Body.Text := 'the body is going to test';
  MailMessages.ReceiptRecipient.Address := 'ehsan.hesam13@gmail.com';

  try
    try
     SMTP.Connect;
     SMTP.Authenticate;
     SMTP.Send(MailMessages);
    except on E:Exception do
      StatusMemo.Lines.Insert(0, 'ERROR: ' + E.Message);
    end;
  finally
    if SMTP.Connected then SMTP.Disconnect;
  end;
end;

如何在XE2中解决这个问题? thankx

2 个答案:

答案 0 :(得分:4)

确保您填写的是TIdMessage.RecipientsTIdMessage.CCListTIdMessage.BCCList媒体资源。这些是TIdSMTP获取其SMTP RCPT TO命令的地址的属性。您无法在未指定收件人的情况下发送电子邮件。您只填写TIdMessage.ReceiptRecipient属性,该属性仅用于指定收件人发送已读回执的返回地址,如果收件人支持读取收件人。

此外,您无需手动拨打Authenticate()Send()在需要时为您内部调用。

答案 1 :(得分:1)

谢谢大家,我填写了Recipients.EMailAddresses并且它有效:D  这是正确的代码:

  SMTP.Host := 'host.com';
  SMTP.Port := 25;
  SMTP.Username:= 'user@host.com';
  SMTP.Password:= 'pass';
  MailMessages.From.Address:='address@address.com';
  MailMessages.From.Name:= 'Ehsan';
  MailMessages.Subject :=  'Test';
  MailMessages.Body.Text := 'the body is going to test';
  MailMessages.Recipients.EMailAddresses:='ehsan.hesam13@gmail.com';

  try
    try
     SMTP.Connect;
     SMTP.Authenticate;
     SMTP.Send(MailMessages);
    except on E:Exception do
      StatusMemo.Lines.Insert(0, 'ERROR: ' + E.Message);
    end;
  finally
    if SMTP.Connected then SMTP.Disconnect;
  end;
end;

再次感谢你。