加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 09:17:36 所属栏目:大数据 来源:网络整理
导读:拜托,我现在试图解决这个“问题”超过12个小时……而且几乎要疯了!我认为不可能使用Delphi和Synapse( http://synapse.ararat.cz)同时发送与收件人(目的地)相同的电子邮件.拜托,有人告诉我,我错了:) 好吧,我有一个sEmail变量,在那里我得到用分号(;)分隔的电
拜托,我现在试图解决这个“问题”超过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中添加其余的电子邮件.

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

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

谢谢!

解决方法

根据 documentation for SendTo

Send “Maildata” (text of e-mail without any SMTP headers!) from “MailFrom” e-mail address to “MailTo” e-mail address with “Subject”. (If you need more then one receiver,then separate their addresses by comma).

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

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编码附件,又支持使用gmail所需的TLS.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读