Send mail with different reply-to address – part 2

Like the in cu 400 used mailing .net assembly the one used in cu 397 has very basic functionality, assembly Microsoft.Dynamics.Nav.Integration.Office. So if you want to use additional properties like setting a different reply-to address or using as different sender address, it is needed write it’s own code using basic .net assemblies. In  part 1 of this series i showed how to send smtp mails with the opportunity to set a different reply-to address. In this part i who, how to get the same with oulook based functionalities.

// variablesolApp, DotNet, Microsoft.Office.Interop.Outlook.ApplicationClass.'Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'olMailItem, DotNet, Microsoft.Office.Interop.Outlook.MailItem.'Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'olItemType, DotNet, Microsoft.Office.Interop.Outlook.OlItemType.'Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'SendOLMail()IF NOT ISNULL(olApp) THEN  CLEAR(olApp);olApp := olApp.ApplicationClass;olMailItem := olApp.CreateItem(olItemType.olMailItem);olMailItem."To" := 'receiver@test.com';olMailItem.Subject := 'subject text';olMailItem.Body := 'This is the message.';// set reply-to addressolMailItem.ReplyRecipients.Add('replyAddress@test.com');olMailItem.Send;CLEAR(olMailItem);CLEAR(olApp);

for using a different sender address in outlook mails follow this post.

cheers

 

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 variablesSmtpClient, 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 variablesFromAddress, 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 addressFromAddress := FromAddress.MailAddress(SenderAddress, SenderName);// create mail messageIF 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-addressReplyAddress := ReplyAddress.MailAddress(ReplyToAddress, ReplyToName);MailMessage.ReplyTo := ReplyAddress;SendSmtpMail()// local variablesSMTPMailSetup, Record, SMTP Mail SetupNetworkCredential, 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 THENIF "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 BEGINCLEAR(NetworkCredential);SmtpClient.Credentials := NetworkCredential;SmtpClient.UseDefaultCredentials := FALSE;  END;  IF ISNULL(MailMessage) THENERROR('Mail msg was not created');  SmtpClient.Send(MailMessage);  MailMessage.Dispose;  SmtpClient.Dispose;  CLEAR(MailMessage);  CLEAR(SmtpClient);END;

The Smtp Setup:

Mail sent 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