Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Avoid to send email to contacts with same e-mail

(0) ShareShare
ReportReport
Posted on by 273

Hi all,

Imagine this possible situation

I have contact A with e-mail X, and contact B with same e-mail X.
I'm making a a function to go though all contacts and send an email, but in this case i don't wan't that contact B receive another e-mail.

Contact.RESET;
Contact.SETRANGE("No.","No.");
Contact.SETFILTER(Contact."E-Mail",'<>%1','');

IF Contact.FINDSET THEN REPEAT 
  Recipients:=Contact."E-Mail";




//Recipients:='roliveira@sameca.com';//
SMTPMailSetup.GET;
SMTPMail.CreateMessage(
  '',
  'roliveira@sameca.com',
  Recipients,
  TestMailTitleTxt1,
  STRSUBSTNO(
    MailBody,
    USERID),TRUE);
SMTPMail.Send;

UNTIL Contact.NEXT=0;


Im thinking of using a temp table and whenever an e-mail is sent the address will be inserted in that table and then check before send the e-mail if it already exists in the table.

Would like to read some opinions. Thanks

*This post is locked for comments

  • Rikarddo Profile Picture
    273 on at
    RE: Avoid to send email to contacts with same e-mail

    Thanks for the help! All the answers are great and now i have the "know how" to finish it.

  • Verified answer
    Olister Rumao Profile Picture
    3,959 on at
    RE: Avoid to send email to contacts with same e-mail

    Hi Rikarddo,

    All you need to do is sort the Contact By Email.

    Store the Currently used Contact Email in a variable and then check if the variable value is not equal to the next contact record.

    In the following code I've created a variable called as tempemail of the type Text and use the code

    TempEmail := '';

    Contact.RESET;

    Contact.SETRANGE("No.","No.");

    Contact.SETFILTER(Contact."E-mail",'<>%1','');

    Contact.SETCURRENTKEY(Contact."E-mail");

    Contact.ASCENDING(TRUE); //Sort by E-mail

    IF Contact.FINDSET THEN

    REPEAT

     IF TempEmail <> Contact."E-mail" THEN BEGIN

     //Your Emailing Code

      Recipients:=Contact."E-Mail";

      SMTPMailSetup.GET;

      SMTPMail.CreateMessage('','roliveira@sameca.com',Recipients,TestMailTitleTxt1,

       STRSUBSTNO(MailBody,USERID),TRUE);

     SMTPMail.Send;

     TempEmail := Recipients;

     END;

    UNTIL Contact.NEXT=0;

  • Verified answer
    Suresh Kulla Profile Picture
    47,789 on at
    RE: Avoid to send email to contacts with same e-mail

    Yes you can do that, after sending the email store the contact in the temporary contact table, you don't need to create another table for this purpose and before sending the email just check in the temporary table if there is a record with that email, if not send the email and insert the record.

  • Verified answer
    Andrey Baludin Profile Picture
    3,941 on at
    RE: Avoid to send email to contacts with same e-mail

    Hello!

    I would create a temporary table with only text field (because I can't remember any existing table with key field of text or long code type).

    After that i would use next conctruction:

    Contact.RESET;
    Contact.SETRANGE("No.","No.");
    Contact.SETFILTER(Contact."E-Mail",'<>%1','');
    
    IF Contact.FINDSET THEN REPEAT 

    temprecord.init;

    temprecord.keyfield := Contact."E-mail";

    if temprecord.INSERT THEN BEGIN

    SMTPMailSetup.GET;

    SMTPMail.CreateMessage( '', 'roliveira@sameca.com', Contact."E-mail", TestMailTitleTxt1, STRSUBSTNO( MailBody, USERID),TRUE);

    SMTPMail.Send;

    END;

    UNTIL Contact.next = 0;

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics NAV (Archived)

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 2

#1
mmv Profile Picture

mmv 2

#1
Amol Salvi Profile Picture

Amol Salvi 2

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans