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 :
Dynamics 365 Community / Blogs / Franz Kalchmair Blog / Send Outlook Mail with diff...

Send Outlook Mail with different Sender Address

keoma Profile Picture keoma 32,729

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


Filed under: c/al, mail, nav 2009, nav 2013 Tagged: c/al, mail, nav 2009, nav 2013

Comments

*This post is locked for comments