如何使用Delphi和Synapse一次为多个收件人发送电子邮件?

时间:2013-03-24 21:30:14

标签: delphi email synapse

拜托,我现在试图解决这个“问题”超过12个小时......而且几乎要疯了!我认为不可能使用Delphi和Synapse(http://synapse.ararat.cz)一次发送相同的电子邮件而不是收件人(目的地)。请有人告诉我我错了:)

好吧,我有一个sEmail变量,我用分号(;)分隔电子邮件,就像这样:

sEmails := 'email1@test.com.br;email2@teste.com.br';

以下是我正在使用的代码:

dSMTP := TSMTPSend.Create;
dSMsg := TMimeMess.Create;
Try 
  With dSMsg, Header Do
  Begin 
    Date := Now;
    Priority := mp_Normal;
    CharsetCode := ISO_8859_1;
    From := 'email@gmail.com';
    ToList.Delimiter := ';';
    ToList.DelimitedText := sEmails;
    Subject := 'Message Subject';
    dPart := AddPartMultipart('mixed', nil);
    AddPartHTML('<h1>Message Text</h1>', dPart);
    EncodeMessage;
  end;
  With dSMTP Do
  Begin
    TargetHost := 'smtp.gmail.com';
    TargetPort := '587';
    AutoTLS := True;
    UserName := 'email@gmail.com';
    Password := 'password';
    Try
      If Login Then
      Begin
        If MailFrom('email@gmail.com', Length('email@gmail.com')) Then
          If MailTo(sEmails) Then MailData(dSMsg.Lines);
        Logout;
      end;
    Except
      On E: Exception Do ShowMessage(E.Message);
    end;
  end;
Finally
  dSMsg.Free;
  dSMTP.Free;
end;

我已经尝试过这样:

If Login Then
Begin
  If MailFrom('email@gmail.com', Length('email@gmail.com')) Then
    If MailTo(dSMsg.Header.ToList[0]) Then MailData(dSMsg.Lines);
  Logout;
end;

...但只发送了第一封电子邮件:(即使在Header.CCList中添加其余的电子邮件。

在另一个测试中,我尝试用逗号(,)更改点分号,同样的问题......

拜托,拜托,有人能说出我做错了吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

根据documentation for SendTo

  

将“Maildata”(没有任何SMTP标题的电子邮件文本!)从“MailFrom”电子邮件地址发送到带有“主题”的“MailTo”电子邮件地址。 (如果您需要多个接收器,请用逗号分隔它们的地址。)

所以这样的应该有效(但见下文,因为它显然不是):

sEMails := 'joe@gmail.com,fred@gmail.com,mary@gmail.com';
....

if MailTo(sEMails) then 
  MailData(dSMsg.Lines);

似乎无法在SMTPSend组件中正确设置多个电子邮件地址。你必须单独发送每个。但是,您可以比自己解析地址更容易,因为您之前已经在代码中将它们添加到dSMsg.Header.ToList

// Declare i as an integer variable, and post all the send to addresses
// one at a time to the MailTo() function
for i := 0 to dSMsg.Header.ToList.Count - 1 do
   MailTo(dMsg.Header.ToList[i]);

// Now send the mail body
MailData(dSMsg.Lines)

IMO,Synapse SMTP支持水平太低,无法轻易使用,除非您出于某种原因特别需要低级支持。 Indy(预装Delphi)和ICS都提供了更简单的SMTP实现,既支持文本和HTML电子邮件以及MIME编码的附件,又支持使用TLS所需的TLS。的Gmail。

答案 1 :(得分:0)

以下是我发送给多个收件人的代码示例。 首先,您需要设置TMimeMess(在此示例中为var Mime)。

uses
  mimemess, smtpsend, mimepart, synautil; //not sure if this list is complete

var 
  SMTP: TSMTPSend;
  s, t: string;
  Mime: TMimeMess;

...

{ log in to SMTP server }
if SMTP.Login then
begin
  { set sender address and total send size }
  if SMTP.MailFrom(synautil.GetEmailAddr({your from address}), 
    Length(Mime.Lines.Text)) then
  begin
    s := {your comma separated e-mail string};

    { add all recipient addresses }
    repeat
      { split addresses by comma and send to each }
      t := synautil.GetEmailAddr(Trim(FetchEx(s, ',', '"')));
      if (t <> '') then
        Result := SMTP.MailTo(t);

      if not Result then
      begin
        //handle failure if necessary here

        Break;
      end;
    until s = '';

    { now send e-mail content }
    if Result then
      Result := SMTP.MailData(Mime.Lines);
  end;

  { and log out }
  SMTP.Logout;
相关问题