Question

Please, I am trying to solve this "problem" for more than 12 hours now... and almost getting crazy! I think is impossible send the same e-mail for more than a recipient (destination) at once, using Delphi and Synapse (http://synapse.ararat.cz). Please, someone tell me I am wrong :)

Well, I have a sEmail variable, where I get the e-mails separated by point semicolon (;), just like this:

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

Here is the code I am using:

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;

I already tried like this:

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;

... but then only the first e-mail was sent :( Even if add the rest of e-mails in the Header.CCList.

In another test I tried to change point semicolon for comma (,), with same problem...

Please, please, can someone tells what I am doing wrong?

Thank you!

Was it helpful?

Solution

According to the 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).

So something like this should work (but see below, because it doesn't apparently):

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

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

It seems that there isn't a way to set multiple email addresses properly in the SMTPSend component. You have to send each individually. You can, however, do it easier than parsing the addresses yourself, since you've already added them to dSMsg.Header.ToList earlier in your code:

// 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, the Synapse SMTP support is too low level to use easily unless you specifically need that low level support for some reason. Both Indy (which comes with Delphi pre-installed) and ICS provide much easier implementation of SMTP, both support both text and HTML emails and MIME-encoded attachments, and both support the TLS needed for working with gmail.

OTHER TIPS

Here is a sample of code how I am sending to multiple recipients. First you need to setup your TMimeMess (var Mime in this example).

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top