web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

NAV 2015 SMTP Mail with Reply-To Address

(0) ShareShare
ReportReport
Posted on by

HI,

does anyone knows how to add a Reply-To Address into a SMTP Mail?

I'm creating a mail with code like this:

...

SMTPMail@1000000009 : Codeunit 400;

...
SMTPMail.CreateMessage("From Name", "From Address", "To Address","Subject Line", '', "HTMl formatted");
SMTPMail.AppendBody(GetBodyText);
SMTPMail.AddCC("Copy-To Address");
SMTPMail.AddBCC("BCC-To Address");

This works fine. 

Now I'm searching to a methode for adding Reply-To Address. In Codeunit 400 there were only .NET Class "Microsoft.Dynamics.Nav.SMTP.SmtpMessage" without any method or property to set this information.

Any idea how to do?

kr

Ahmet

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    keoma Profile Picture
    32,729 on at

    in cu 400 .net assembly Microsoft.Dynamics.Nav.SMTP is used to create a mail message. it does not contain a "reply-to address" property. in cu 397 variable OutlookMessageHelper, assembly Microsoft.Dynamics.Nav.Integration.Office, is used to create mail messages. it also does not provide a "reply-to address" property. so you need a custom solution.

    // variables

    olMailItem DotNet Microsoft.Office.Interop.Outlook.MailItem.'Microsoft.Office.Interop.Outlook, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

    olMailItem := olApp.CreateItem(olItemType.olMailItem);

    olMailItem.Subject := 'subject text';

    olMailItem."To" := 'receiver@test.com';

    olMailItem.Body := 'This is the message.';

    // set repl-to address

    olMailItem.ReplyRecipients.Add('replyAddress@test.com');

  • Community Member Profile Picture
    on at

    Hi jonathan,

    thank's for your answer.

    I think, this will be working.

    But, with your custom solution I've to modify the complete mail creation and send functions from CU 400.

    I'm not a practical with c/al in combination with .NET.

    Is there a chance to put the configured mail from CU 400 to an other .NET Class????

    I've catched somehing like that.

    Maybe like this:

    //variables

    SMTPMail@1000000009 : Codeunit 400;

    olMailItem DotNet Microsoft.Office.Interop.Outlook.MailItem.'Microsoft.Office.Interop.Outlook, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

    //Code

    ...

    //Create mail in NAV with default Codeunit 400

    SMTPMail.CreateMessage("From Name", "From Address", "To Address","Subject Line", '', "HTMl formatted");

    SMTPMail.AppendBody(GetBodyText);

    SMTPMail.AddCC("Copy-To Address");

    SMTPMail.AddBCC("BCC-To Address");

    // The next lines I don't know how or if this works....

    // olMailItem or another default mail class will get the customed Mail from

    // NAV class with all Information and after setting reply-to,

    // the NAV Mail Class will get this Mail back for sending

    // so it can be only one new function to set the reply-to address

    //put this mail to the other class

    olMailItem := olApp.CreateItem(olItemType.olMailItem);

    olMailItem := SMTPMail;

    // set repl<-to address

    olMailItem.ReplyRecipients.Add('replyAddress@test.com');

    //put this mail back to the NAV class and send

    SMTPMail := olMailItem;

    SMTPMail.send;

    I'm afraid for an answer.

    KR

    Ahmet

  • Verified answer
    keoma Profile Picture
    32,729 on at

    for a solution follow moxie4nav.wordpress.com/.../send-mail-with-different-reply-to-address

    i've developed a codeunit for that issue on base of the mailing .net classes.

  • Community Member Profile Picture
    on at

    Hi Jonathan,

    thx for information. With this .net class it works to set a reply-to address. Now the last issue is to catch an error by sending the generated mail. Method "Send" in SmtpClient-class has no return-value. Do you have an idea to catch an error here?

    If there is a chance for this i want to modify all functions in cu 400. (within TrySend and Send)

    br

    Ahmet

  • Verified answer
    keoma Profile Picture
    32,729 on at

    hi ahmet,

    that can be done with try-catch.

    for that follow

    vjeko.com/try-catch-in-cal

    code4nav.com/.../how-to-try-catch-in-cal-for-nav2016

    msdn.microsoft.com/.../dn951443(v=nav.90).aspx

  • Community Member Profile Picture
    on at

    thank's a lot. This will also helpful by developing in NAV 2016.

    Now I'll hope the last problem will solved with any idea.

    I've tested a lot of variants to add an attachment by using .NET System.Net.Mail.MailMessage. Nothing will work.

    With adding attachments, method Send will give an error:

    (GER): "Fehler bei einem Aufruf von System.Net.Mail.SmtpClient.Send mit folgender Meldung: Auf eine geschlossene Datei kann nicht zugegriffen werden.

    (ENU): "A call to System.Net.Mail.SmtpClient.Send failed with this message: Cannot access a closed file.

    Here is the code to generate the mail with attachment(last two lines in CreateMessage) and send it. Without attachment it works fine.

    Variables:

    SmtpClient DotNet System.Net.Mail.SmtpClient.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    MailMessage DotNet System.Net.Mail.MailMessage.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    FromAddress DotNet System.Net.Mail.MailAddress.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    ReplyAddress DotNet System.Net.Mail.MailAddress.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    Attachment DotNet System.Net.Mail.Attachment.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    ...

    ...

    //Create MailMessage

    SendResult := '';

    MailMessage := MailMessage.MailMessage;

    FromAddress := FromAddress.MailAddress(SenderAddress,SenderName);

    MailMessage.From := FromAddress;

    MailMessage."To".Clear;

    MailMessage."To".Add(Recipients);

    MailMessage.Subject := Subject;

    IF HtmlFormatted THEN

     MailMessage.Body := LocalMail.FormatTextForHtml(Body)

    ELSE

     MailMessage.Body := Body;

    MailMessage.IsBodyHtml := HtmlFormatted;

    ReplyAddress := ReplyAddress.MailAddress(ReplyToAddress,ReplyToName);

    MailMessage.ReplyTo := ReplyAddress;

    //Add Attachment

    Attachment := Attachment.Attachment('G:\sample.pdf');

    MailMessage.Attachments.Add(Attachment);

    //Send MailMessage

    SMTPMailSetup.GET;

    WITH SMTPMailSetup DO BEGIN

     IF NOT ISNULL(SmtpClient) THEN BEGIN

       SmtpClient.Dispose;

       CLEAR(SmtpClient);

     END;

     SmtpClient := SmtpClient.SmtpClient("SMTP Server","SMTP Server Port");

     SmtpClient.EnableSsl := "Secure Connection";

     CASE Authentication OF

       Authentication::Anonymous:

       BEGIN

         CLEAR(NetworkCredential);

         SmtpClient.Credentials := NetworkCredential;

         SmtpClient.UseDefaultCredentials := FALSE;

       END;

       Authentication::NTLM:

       BEGIN

         CLEAR(NetworkCredential);

         SmtpClient.Credentials := NetworkCredential;

         SmtpClient.UseDefaultCredentials := TRUE;

       END;

       Authentication::Basic:

       BEGIN

         IF "User ID" <> '' THEN BEGIN

           SmtpClient.UseDefaultCredentials := FALSE;

           NetworkCredential := NetworkCredential.NetworkCredential("User ID", Password);

           SmtpClient.Credentials := NetworkCredential;

         END ELSE BEGIN

           CLEAR(NetworkCredential);

           SmtpClient.Credentials := NetworkCredential;

           SmtpClient.UseDefaultCredentials := TRUE;

         END

       END;

     END;

     IF ISNULL(MailMessage) THEN BEGIN

    //    ERROR('Mail msg was not created');

       SendResult := 'Mail was not created';

       EXIT(FALSE);

     END;

     //with attachment send will give an error

     SmtpClient.Send(MailMessage);

     MailMessage.Dispose;

     SmtpClient.Dispose;

     CLEAR(MailMessage);

     CLEAR(SmtpClient);

    END;

  • keoma Profile Picture
    32,729 on at

    does it work without attachment code lines ?

    the error is given, if the filestream to get the attched file is closed to early ... before send process is finished. check, if the smtpclient instance is working fine and sends a mail correctly without attachment.

  • Community Member Profile Picture
    on at

    yes, without the two lines

    //Add Attachment

    Attachment := Attachment.Attachment('G:\sample.pdf');

    MailMessage.Attachments.Add(Attachment);

    it works fine. 

    In moment I have no idea how to do with attachment.

    Other tests with System.Net.Mail.AttachmentCollection, System.IO.FileStream, System.IO.FileMode also doesn't work in NAV 2015, maybe my syntax is wrong.

     

     

  • Verified answer
    Community Member Profile Picture
    on at

    now I have solved the problem, the last issue was not a problem with adding an attachment.

    In my database I had added two or more attachments with the same dotnet variable.

    That doesn't work.

    If you have more than one attachment you have to instance an attachment class for every file, then it works.

    Here is a sample with three attachments.

    Sample Code:

    global variables----

    SMTPMailSetup Record SMTP Mail Setup

    SendResult Text

    SmtpClient DotNet System.Net.Mail.SmtpClient.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    NetworkCredential DotNet System.Net.NetworkCredential.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    MailMessage DotNet System.Net.Mail.MailMessage.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    FromAddress DotNet System.Net.Mail.MailAddress.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    ReplyAddress DotNet System.Net.Mail.MailAddress.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    Attachment DotNet System.Net.Mail.Attachment.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    Attachment1 DotNet System.Net.Mail.Attachment.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    Attachment2 DotNet System.Net.Mail.Attachment.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    Mail Codeunit Mail

    variables++++

    Code----

    OnRun()

    // Test mail function

    CreateMessageRT('sender name','navsmtptest@sample.localExchangeaccount','Ahmet.Gezer@myprovider.de','Testmail','Bodytext',FALSE,'ReplyTo-Name','ReplyTo@sample.de');

    TrySendRT;

    CheckValidEmailAddresses(Recipients : Text)

    Local variables----

    MailManagement Codeunit Mail Management

    Local variables++++

    MailManagement.CheckValidEmailAddresses(Recipients);

    CreateMessageRT(SenderName : Text;SenderAddress : Text;Recipients : Text;Subject : Text;Body : Text;HtmlFormatted : Boolean;ReplyToName : Text;ReplyToAddress : Text)

    IF Recipients <> '' THEN

     CheckValidEmailAddresses(Recipients);

    CheckValidEmailAddresses(SenderAddress);

    SMTPMailSetup.GET;

    SMTPMailSetup.TESTFIELD("SMTP Server");

    IF NOT ISNULL(MailMessage) THEN BEGIN

     MailMessage.Dispose;

     CLEAR(MailMessage);

    END;

    SendResult := '';

    MailMessage := MailMessage.MailMessage;

    FromAddress := FromAddress.MailAddress(SenderAddress,SenderName);

    MailMessage.From := FromAddress;

    MailMessage."To".Clear;

    MailMessage."To".Add(Recipients);

    MailMessage.Subject := Subject;

    IF HtmlFormatted THEN

     MailMessage.Body := Mail.FormatTextForHtml(Body)

    ELSE

     MailMessage.Body := Body;

    MailMessage.IsBodyHtml := HtmlFormatted;

    ReplyAddress := ReplyAddress.MailAddress(ReplyToAddress,ReplyToName);

    MailMessage.ReplyTo := ReplyAddress;

    Attachment := Attachment.Attachment('C:\temp\Sample.pdf');

    MailMessage.Attachments.Add(Attachment);

    Attachment1 := Attachment1.Attachment('C:\temp\Sample1.pdf');

    MailMessage.Attachments.Add(Attachment1);

    Attachment2 := Attachment2.Attachment('C:\temp\Sample2.pdf');

    MailMessage.Attachments.Add(Attachment2);

    LOCAL TrySendRT() : Boolean

    SMTPMailSetup.GET;

    WITH SMTPMailSetup DO BEGIN

     IF NOT ISNULL(SmtpClient) THEN BEGIN

       SmtpClient.Dispose;

       CLEAR(SmtpClient);

     END;

     SmtpClient := SmtpClient.SmtpClient("SMTP Server","SMTP Server Port");

     SmtpClient.EnableSsl := "Secure Connection";

     CASE Authentication OF

       Authentication::Anonymous:

       BEGIN

         CLEAR(NetworkCredential);

         SmtpClient.Credentials := NetworkCredential;

         SmtpClient.UseDefaultCredentials := FALSE;

       END;

       Authentication::NTLM:

       BEGIN

         CLEAR(NetworkCredential);

         SmtpClient.Credentials := NetworkCredential;

         SmtpClient.UseDefaultCredentials := TRUE;

       END;

       Authentication::Basic:

       BEGIN

         IF "User ID" <> '' THEN BEGIN

           SmtpClient.UseDefaultCredentials := FALSE;

           NetworkCredential := NetworkCredential.NetworkCredential("User ID", Password);

           SmtpClient.Credentials := NetworkCredential;

         END ELSE BEGIN

           CLEAR(NetworkCredential);

           SmtpClient.Credentials := NetworkCredential;

           SmtpClient.UseDefaultCredentials := TRUE;

         END

       END;

     END;

     IF ISNULL(MailMessage) THEN BEGIN

       SendResult := 'Mail was not created';

       EXIT(FALSE);

     END;

     SmtpClient.Send(MailMessage);

     MailMessage.Dispose;

     SmtpClient.Dispose;

     CLEAR(MailMessage);

     CLEAR(SmtpClient);

    END;

    Code++++

  • Community Member Profile Picture
    on at

    Even after using same set of codes, I am getting the error "A call to System.Net.Mail.SmtpClient.Send failed with this message: Cannot access a closed file."

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

#1
HoangNam Profile Picture

HoangNam 7

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans