Import mail (msg) in Navision

elwin68elwin68 Member Posts: 153
For a customer I have made a solution so files can be imported into a BLOB in Navision. The most files that will be imported are emails from outlook.
Now the customer goes to outlook and saves a email to a msg-file. After that he goes to Navision and imports the msg-file. Finally the msg-file will be deleted.

I wonder if it's possible through automation to do this in 1 step from Navision.
My idea is to push a button in Navision, show an overview with outlook-emails and after selection import an email as a msg-file into Navision.

Had someone has experience with this?

Thanks in advance.

Answers

  • rvduurenrvduuren Member Posts: 92
    Hello Elwin,

    I found some C# code and it's working here..
    Original link -> .NET Connector for Microsoft Outlook, vb code is also available :)

    The idea:
    1. Get header data of the Inbox mails from Outlook in XML.
    2. Display the XML in Dynamics NAV (import in temp table and show on form).
    3. With the messageID you receive the mail as a file.
    4. Import the file in Dynamics NAV, and youre done..

    Known issues:
    1. The user receives a confirm in Outlook to allow access.
    2. Receiving the mail as a file is slow b/c it loops the mailbox(try using find).

    Here are my modifications:
    using System;
    using System.Data;
    using System.Diagnostics;
    
    namespace Magi.OutlookConnector
    {
    	/// <summary>
    	/// Delegate declaration for each time an item is processed from an Outlook folder.
    	/// </summary>
    	public delegate void OutlookItemProcessed();
    
    	/// <summary>
    	/// This disposable class acts a translator for information stored in the user's Outlook folders.
    	/// </summary>
    	public class OutlookConnector : IDisposable
    	{
    		private Microsoft.Office.Interop.Outlook.Application objOutlook = null;
            private Microsoft.Office.Interop.Outlook.NameSpace objNamespace = null;
            private Microsoft.Office.Interop.Outlook.MAPIFolder objFolder = null;		
    
    		public OutlookConnector()
    		{
    			objOutlook = new Microsoft.Office.Interop.Outlook.ApplicationClass();
    			objNamespace = objOutlook.GetNamespace("MAPI");
    		}
    
    		/// <summary>
    		/// Close the Outlook application when this instance is disposed.
    		/// </summary>
    		public void Dispose()
    		{
                if (objOutlook != null) { objOutlook.Quit(); };
    		}
    
    
    		/// <summary>
    		/// Retrieves a list of all emails in the Outlook Inbox.
    		/// </summary>
    		/// <returns>Inbox E-Mail Items DataSet</returns>
    		public DataSet getInboxDataSet()
    		{
                Microsoft.Office.Interop.Outlook.MailItem item;
    			DataSet rv = new DataSet();
    			rv.DataSetName = "InboxEmails";
    			rv.Tables.Add("Email");
    			rv.Tables[0].Columns.Add("From");
    			rv.Tables[0].Columns.Add("To");
    			rv.Tables[0].Columns.Add("Cc");
    			rv.Tables[0].Columns.Add("Subject");
    			rv.Tables[0].Columns.Add("Received");
                rv.Tables[0].Columns.Add("MessageID");
    
    			try
    			{
    				objFolder = objNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
    
                    System.Collections.IEnumerator MsgEnum = objFolder.Items.GetEnumerator();
                    while (MsgEnum.MoveNext())
                    {
                        item = (Microsoft.Office.Interop.Outlook.MailItem)MsgEnum.Current;                    
                        rv.Tables[0].Rows.Add(new object[] { item.SenderName, item.To, item.CC, item.Subject, item.ReceivedTime, item.EntryID});                                        
                    }				
    			}
    			catch (System.Exception e)
    			{
    			}
    			return rv;
    		}
            public void getMail(string entryId, string fileName)
            {
                Microsoft.Office.Interop.Outlook.MailItem item;
    
    			try
    			{
    				objFolder = objNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
    
                    System.Collections.IEnumerator MsgEnum = objFolder.Items.GetEnumerator();
                    while (MsgEnum.MoveNext())
                    {
                        item = (Microsoft.Office.Interop.Outlook.MailItem)MsgEnum.Current;
                        if (item.EntryID == entryId)
                        {
                            item.SaveAs(fileName, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);
                            break;
                        }
                    }	                
                }
    			catch (System.Exception e)
    			{
    			}
            }
    	}
    }
    

    How I use it:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Magi.OutlookConnector;
    using System.Data;
    using System.Xml;
    
    namespace OutlookMsg
    {
        class Program
        {
            static void Main(string[] args)
            {
                OutlookConnector outlookConnector = new OutlookConnector();
                DataSet inboxData = outlookConnector.getInboxDataSet();
                XmlDataDocument xmlDatDoc = new XmlDataDocument(inboxData);
                xmlDatDoc.Save(@"c:\inbox.xml");
                outlookConnector.getMail("00000000C87E48AA6FBB7C43B4A31113A548469207007475BC6C99FA364782D23D53D3034DC000000015A43B00007475BC6C99FA364782D23D53D3034DC00000001F4CB40000", @"c:\tmp.msg");
                outlookConnector.Dispose();
            }
        }
    }
    

    Combine it in a automation control for NAV
    Link

    And you'r done.

    Hope It helped you..
    Met vriendelijke groet, best regards,

    Rvduuren
  • elwin68elwin68 Member Posts: 153
    Hello Rik,

    Nice to hear from you. It's a long time ago.
    Thanks for your answer. I'm gonna try to work with this solution.

    Greetings,
    Elwin.
Sign In or Register to comment.