For sending outlook mails one can use CU 397 and it works fine, if it’s ok to use the standard outlook profile as sender address (“From”). If you want to use a different sender address, then this is not possible.
To get that possibility let’s have a look at the in CU 397 used .net assemblies. There we have especially assembly Microsoft.Dynamics.Nav.Integration.Office. For most cases a nice little thing. But it delivers no possibility to set/change the sender address. So what to do?
Assembly Microsoft.Dynamics.Nav.Integration.Office.dll references (internally) the assembly Microsoft.Office.Interop.Outlook. So let’s have a more precise look on THAT assembly. There we have the typical from COM to .Net converted assembly with strange looking classes like Microsoft.Office.Interop.Outlook.ApplicationClass, etc. But … this class ApplicationClass contains a property Session and further a property Accounts, what means the outlook user profiles. Voila! We have, what we want, the access to the solution. Accounts.item(index) gives us one distinct account, which has the property SmtpAddress, means the mail address of an outlook account. This mail address we will compare with the given sender mail address for sending the mail. What else do we need? The possibility to assign the sender address. So let’s check the mail message class Microsoft.Office.Interop.Outlook.MailItem. There we have property SendUsingAccount to set/assign the sender account. Ok then, let’s do it …
//local 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
Let’s say, we select the sender address from table “company information”.
senderMailAddress := CompInfo."E-Mail" // e.g. 'sender@test.com'; olApp := olApp.ApplicationClass; // creates the outlook instance olMailItem := olApp.CreateItem(olItemType.olMailItem); // creates a new outlook mail message // 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'; // optional: file to attach olMailItem.Attachments.Add(fileName,olAttachmentType.olByValue,1,fileName); olMailItem.Display(TRUE); // Display the outlook window
cheers
I tried it and when I receive the email in outlook and click on reply it is still using my email from the sender. The code is not replacing the sender’s email. Any ideas on how to correct this. Thanks
LikeLike
sorry for the late respond. did you find a solution in the meantime ? is there an oulook profile for the given senderMailAddress? regards
LikeLike
So the prerequisite is that you need to have the profile which you want to send mails from is present locally ? We wanted to use this functionality where a customer service center is sending mails on behalf of one of the 80 stores they have (so that when a customer replies is replies directly to the store instead of the customer service). I don’t think it is feasible to have 80 outlook profiles active …
LikeLike
I am getting an error on line olMailItem := olApp.CreateItem(olItemType.olMailItem);
You have specified an unknow variable olMailItem please help
LikeLike