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)

Mail 397 - using Newmessage but being able to select a user profile

(0) ShareShare
ReportReport
Posted on by 5,136

I am using Mail codenuit (397)'s newmessage function to be able to create a new Outlook Message and have users review the message before sending.  This is working well and good.  However, I want to be able to automatically select a user profile as the sender.  Can I do this programmatically?  E.g., in my Outlook, I actually have two user profiles.  I want my recipients to know that email came from a specific email address.  if I can hardcode this in Nav, it's okay too.  Please advise.

*This post is locked for comments

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

    hi,

    one option: use a strmenu dialog to select the sender mail before executing newmessage

    msdn.microsoft.com/.../dd339017.aspx

    another option: add a new field "Email 2" to the table "company information". develop a logic to use one of the email addresses from the company information as sender mail address.

  • mbr Profile Picture
    5,136 on at

    Hi Jonathan.  I like the option 2: add a new field to use as sender mail address.  However, in the Mail 397 codenunit, using NewMessage function doesn't give me the option to change the sender mail address.  Please advise.  I really like the NewMessage function as I want users to review the message before sending.  However, this function doesn't give me the option to select a sender mail address.

  • Suggested answer
    keoma Profile Picture
    32,729 on at

    hi mbr,

    NewMessage works with:
    variable: sender | Text 

    sender := 'sender@test.com'
    NewMessage(sender,'','subject text','body text','c:\temp\test.docx',TRUE);
    

    to change the sender mailer by configuration you could create the field "Email 2" in the company information table. with an additional field SenderMailAddress (option field with values Email1, Email2) you could select/set the sender by config.

    variable: compInfo | Rec | Company Information

    compInfo.Get;
    if compInfo.SenderMailAddress = compInfo.SenderMailAddress::Email1 then
      sender := compInfo.Email
    else
      sender := compInfo."Email 2";
    NewMessage(sender, ...)

    also possible create a new field CompanySenderMail in the table customer/vendor with a lookup to the sender field in the comp.info. table. so you could set the wanted sender mailer per customer/vendor. or maybe an other logic according to your needs.

  • mbr Profile Picture
    5,136 on at

    Hi Jonathan,

    In the 397 Mail Codeunit (Nav 2013 version), below is the NEwMessage function.  It expects the ToRecipients as the first parameter. :(  If I set the Sender as the first parameter, that goes to the TO field in the Outlook message.  

    NewMessage(ToAddresses : Text;CcAddresses : Text;Subject : Text;Body : Text;AttachFilename : Text;ShowNewMailDialogOnSend : Boolean) : Boolean

    Initialize;

    OutlookMessageHelper.Recipients := ToAddresses;

    OutlookMessageHelper.CarbonCopyRecipients := CcAddresses;

    OutlookMessageHelper.Subject := Subject;

    OutlookMessageHelper.BodyFormat := 2;

    OutlookMessageHelper.ShowNewMailDialogOnSend := ShowNewMailDialogOnSend;

    AddBodyline(Body);

    AttachFile(AttachFilename);

    EXIT(Send);

  • Verified answer
    keoma Profile Picture
    32,729 on at

    hi,

    ok, right. CU 397 uses the .net assembly Microsoft.Dynamics.Nav.Integration.Office. there is no possibility to set/change the sender address for creating a mail message with the classes delivered by that assembly.

    the Microsoft.Dynamics.Nav.Integration.Office.dll references (internally) the assembly  Microsoft.Office.Interop.Outlook.

    using that assembly you can create a mail message and set the sender mail address.

    the variables:

    --------------------------------

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

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

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

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

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

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

    fileName Text

    senderMailAddress Text

    idx Integer

    the code:

    --------------------------

    // here select the sender Address from table company information,

    // fields Email, "Email 2".

    senderMailAddress := 'sender@test.com';

    olApp := olApp.ApplicationClass;

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

    // find the selected outlook profile and set it as sender mailAddress

    olAccountList := olApp.Session.Accounts;

    idx := 1;

    REPEAT

     olAccount := olAccountList.Item(idx);

     IF LOWERCASE(olAccount.SmtpAddress) = LOWERCASE(senderMailAddress) THEN

       olMailItem.SendUsingAccount := olAccount;

     idx += 1;

    UNTIL idx > olAccountList.Count;

    olMailItem.Subject := 'subject text';

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

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

    fileName := 'c:\temp\test.docx';

    olMailItem.Attachments.Add(fileName,olAttachmentType.olByValue,1,fileName);

    olMailItem.Display(TRUE);

  • mbr Profile Picture
    5,136 on at

    Thank you Jonathan. I think that will do it.  'will try this strategy! :)

  • keoma Profile Picture
    32,729 on at

    don't forget to set the dotnet variables property runonclient to yes.

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi.

    I had same problem here and I modified CU 397 with a global variable called OfName and a new method to set this variable.

    Then in NewMessage function I set the code below:

    IF OfName <> '' THEN

       OSendMail.SentOnBehalfOfName := OfName;

    Btw, OfName variable, goes to From Field in Outlook.

    In a report, it's easy to use a variable in Request Form to change this From Field or automatically setting as you like.

    Regards.

    [Edited] 

    Sorry, I didn't read about your Nav version. I was talking about Nav 5.1 or earlier versions.

    I guess you can find some similar property like SentOnBehalfOfName to resolve this.

  • keoma Profile Picture
    32,729 on at

    hi,

    nice idea, but only working for nav 5.

    the variable OSendMail is of type Automation|"NS Outlook Synchronization Handler".

    this automation is not part of nav 2013 any more. and it is in general not recommended to use automations in nav 2013. external functionality should come from dotnet assemblies.

  • mbr Profile Picture
    5,136 on at

    Yes. And to resolve my issue, I ended up using the .net interop outlook assembly as stated by Jonathan in his response to this issue back in November 2014 and created the mail message that way.  That worked! :)

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)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans