Recent

Author Topic: Multiple email addresses? How to  (Read 4323 times)

Przemyslav

  • New Member
  • *
  • Posts: 44
Multiple email addresses? How to
« on: January 23, 2019, 02:00:05 am »
Hello everybody. I have problem with my program. My program send emails with attachments to multiple email addresses. Emails I have in code -->  Mail.Receivers.Add...

Code: Pascal  [Select][+][-]
  1. Mail := TSendMail.Create;
  2.  
  3.   try
  4.       Mail.Sender := '*************' <'*************'>;
  5.       Mail.Receivers.Add('firstemail@gmail.com');
  6.       Mail.Receivers.Add('secondemail@gmail.com');
  7.       Mail.Subject := 'Hello';
  8.       Mail.Message.Add('How are you');
  9.       Mail.Smtp.UserName := '*************';
  10.       Mail.Smtp.Password := '*************';
  11.       Mail.Smtp.Host := 'smtp.gmail.com';
  12.       Mail.Smtp.Port := '587';
  13.       //Mail.Smtp.SSL := True;
  14.       mail.Smtp.FullSSL:=false;
  15.       Mail.Smtp.TLS := True;
  16.       Mail.Send;
  17.       ShowMessage('SUCCESS :) ');
  18.     finally
  19.       Mail.Free;
  20.     end;

But I would like to have Edit component where I could write multiple email addresses separated with for example comma or semicolon: firstemail@gmail.com; secondemail@gmail.com; ...

Something like in attachment.

But I don't now how to copy separated email addresses from Edit to section Mail.Receivers.Add in my code with comma or semicolon and send to these receivers.

Thank you very much :)


jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Multiple email addresses? How to
« Reply #1 on: January 23, 2019, 02:20:13 am »
The most practice way would be to decompose the string.

For example, Use the function POS which will return return the first ";" it finds

this will give you an index.

Use the "LeftStr" function or Copy to another string, this will be the first one in the list..

you then add it to your other list.

Delete this string from the current composite string including the ";" and repeat the cycle

if it comes to a point where POS does not find a ";" then test the final results of this string and use that one
for the final.

 it could be that you only enter one and POS will not find the ";" so at this point it is the final string.
----

 Another way is to list a loop and control a string from the master until you hit the end of the master string or
a ";"
WorkString := '';
For I := 1 to Length(MasterString) do
 Begin
   If MasterString[ I ] <> ';' Then Workstring := Workstring+MasterString[ I ] else
     If WorkString <> '' Then
     begin
       Mail.AddToMYList(WorkString);
       WorkString := '';
     End;
 if WorkString <> '' Then Mail.AddToMyList(WorkString);

I think you get the idea.

P.S.
 You can also use a TstringList and set the Linebreak character which would be ";" and if I understand that
correctly it should beak the line up when you assign the test..
Stringlist.LineBreal := ';';

StringList.Assign(MasterString);

now the Items should be separated, maybe  :o
« Last Edit: January 23, 2019, 02:30:14 am by jamie »
The only true wisdom is knowing you know nothing

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Multiple email addresses? How to
« Reply #2 on: January 23, 2019, 07:29:06 pm »
TSendMail.Receivers is a TStrings descendant, TStringList is also a TStrings descendant. TStringList can be set to use Delimiter (, or ;, you choose), preferably with StrictDelimiter to true but it depends on you as well whether you want to allow whitespaces or not, then just assign that delimiter separated long string of email addresses to DelimitedText property. You can then simply call TSendMail.Receivers.Assign(ThatTStringListInstanceWhoseDelimitedTextPropertyYouJustAssign), done.

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: Multiple email addresses? How to
« Reply #3 on: January 29, 2019, 06:06:03 pm »
Because I'm so stupid I can't do this. I tried with my example e-mails:

Code: Pascal  [Select][+][-]
  1. joe@gmail.com;fred@gmail.com;mary@gmail.com
and delimiter but it's to hard for me :(

Thanks you for your help.

.......

I have to try this:

Code: Pascal  [Select][+][-]
  1. Mail.Receivers.Add('to=dest1@host.com;dest2@host.com');
  2.  
« Last Edit: January 29, 2019, 06:10:21 pm by Przemyslav »

CCRDude

  • Hero Member
  • *****
  • Posts: 600
Re: Multiple email addresses? How to
« Reply #4 on: January 29, 2019, 06:25:04 pm »
Using delimiters is not that complicated; here's the three important properties:
Code: Pascal  [Select][+][-]
  1. Mail.Receivers.StrictDelimiter := true;
  2. Mail.Receivers.Delimiter := ';';
  3. Mail.Receivers.DelimitedText := 'joe@gmail.com;fred@gmail.com;mary@gmail.com';

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: Multiple email addresses? How to
« Reply #5 on: January 29, 2019, 06:32:49 pm »
OK, now I get this. I try to make list in txt file and import to String :) Thanks :)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Multiple email addresses? How to
« Reply #6 on: January 30, 2019, 08:30:29 am »
No, you don't. the mailadresses can be used as an variable. If you have an editbox with several adresses you can use this
Code: Pascal  [Select][+][-]
  1. Mail.Receivers.DelimitedText := Editbox.txt
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Multiple email addresses? How to
« Reply #7 on: February 04, 2019, 11:37:40 am »
No, you don't. the mailadresses can be used as an variable. If you have an editbox with several adresses you can use this
Code: Pascal  [Select][+][-]
  1. Mail.Receivers.DelimitedText := Editbox.txt
Ah, didn't realize DelimitedText is part of TStrings instead of TStringList.

 

TinyPortal © 2005-2018