Send Mail with different Reply-To Address

The properties of the used .net assembly Microsoft.Dynamics.Nav.SMTP in CU 400 is very basic, e.g. applying a different Repl-To Address is not possible. For that it’s needed to go to the basics, means use the basic .net classes for Mailing. Following codeunit is a solution for that issue.

// global variables
SmtpClient, DotNet, System.Net.Mail.SmtpClient.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
MailMessage, DotNet, System.Net.Mail.MailMessage.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

OnRun()
// Test the mail functions using a sample gmx mail account 'your_account@gmx.at'
CreateMessage('sender name','your_account@gmx.net','your_account@gmx.net','subject','message text','reply name','replyTo@gmx.net');
SendSmtpMail;

CreateMessage(SenderName : Text;SenderAddress : Text;Recipients : Text;Subject : Text;Body : Text;ReplyToName : Text;ReplyToAddress : Text)
// local variables
FromAddress, DotNet, System.Net.Mail.MailAddress.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
ReplyAddress, DotNet, System.Net.Mail.MailAddress.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

// create from address
FromAddress := FromAddress.MailAddress(SenderAddress, SenderName);

// create mail message
IF NOT ISNULL(MailMessage) THEN
  CLEAR(MailMessage);
MailMessage := MailMessage.MailMessage;
MailMessage.From := FromAddress;
MailMessage.Body := Body;
MailMessage.Subject := Subject;
MailMessage."To".Clear;
MailMessage."To".Add(Recipients);

// create and add reply-to-address
ReplyAddress := ReplyAddress.MailAddress(ReplyToAddress, ReplyToName);
MailMessage.ReplyTo := ReplyAddress;

SendSmtpMail()
// local variables
SMTPMailSetup, Record, SMTP Mail Setup
NetworkCredential, DotNet, System.Net.NetworkCredential.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

SMTPMailSetup.GET;
WITH SMTPMailSetup DO BEGIN
  SmtpClient := SmtpClient.SmtpClient("SMTP Server","SMTP Server Port");
  SmtpClient.EnableSsl := "Secure Connection";
  IF Authentication  Authentication::Anonymous THEN
    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
  ELSE BEGIN
    CLEAR(NetworkCredential);
    SmtpClient.Credentials := NetworkCredential;
    SmtpClient.UseDefaultCredentials := FALSE;
  END;

  IF ISNULL(MailMessage) THEN
    ERROR('Mail msg was not created');

  SmtpClient.Send(MailMessage);
  MailMessage.Dispose;
  SmtpClient.Dispose;

  CLEAR(MailMessage);
  CLEAR(SmtpClient);
END;

The Smtp Setup:
smtp-setup

Mail sent result:
smtpmail-result

you can also use that codeunit for other purposes as template, e.g. for setting a different sender address.

for sending outlook mails with different reply-to address follow part 2 post of that series.

cheers

 

Advertisements

2 thoughts on “Send Mail with different Reply-To Address

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s