Quantcast
Channel: WinDev Forum
Viewing all articles
Browse latest Browse all 1914

EWS extract mail to .eml

$
0
0
Hi guys.

I am using Exchange WebServices to access multiple mailboxes on an Exchange server. Because I want my application to run as a system service, I cannot - and don't want - to use the built in functions as it relies on outlook and I don't want to create a million Outlook profiles... This solution also has the benefit of running from a non-domain connected computer. If it was possible to get working thoroughly, it could be used as the basis for an archival tool or a way to backup multiple Exchange online accounts (o365)

I can log in just fine, list folder contents, load individual messages, save attachments etc. All good.
However, What I really want to do is to save the messages as .EML files. According to the EWS API this can only be done by saving the MIMECONTENT of the message as a whole.

Here is my code (Most of which works great), hope fully someone will find this useful
The bit that doesn't work is the saving of the mimecontent near the end.

If anyone has done this successfully, I'd be delighted if you could share

Cheers, Bosh
ServiceEWS is an ExchangeService(ExchangeVersion.Exchange2013,TimeZoneInfo.Utc)



ServiceEWS.Credentials = new WebCredentials("login","pw","WDomain")
//Note. Wdomain is the NT style domain name, not the public domain name

ServiceEWS.Url = new Uri("[mail.yourdomain.com];);


//Test read of ten messages in Inbox
clInbox is a Folder(ServiceEWS)
clInbox <- Folder.Bind(ServiceEWS,WellKnownFolderName.Inbox)
clFindresults is a 'FindItemsResults<Microsoft.Exchange.WebServices.Data.Item>'
clFindresults <- ServiceEWS.FindItems(WellKnownFolderName.Inbox,new ItemView(10))

clMail is an EmailMessage(ServiceEWS)

FOR ALL i OF clFindresults.Items
	xs is an 'Collection<Microsoft.Exchange.WebServices.Data.PropertyDefinitionBase>'
	xs.add(ItemSchema.Attachments)
	clMail <- EmailMessage.Bind(ServiceEWS,i.id, new PropertySet(BasePropertySet.IdOnly, xs))
	clMail.Load()
	Trace(clMail.Subject)
END

bMoreItems is a boolean = True
ipagesize is int = 50
IOffset is int 
nIIter is int 
nIDspCnt is int 



//Search for subfolders of Inbox
vi is a FolderView = Null
Scfilt is a SearchFilter = Null

Scfilt <- new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);
vi <- new FolderView(ipagesize,IOffset)
vi.PropertySet = new PropertySet(BasePropertySet.IdOnly)
vi.PropertySet.Add(FolderSchema.DisplayName)
vi.PropertySet.Add(FolderSchema.Id)
vi.PropertySet.add(FolderSchema.FolderClass)
vi.Traversal = FolderTraversal.Deep

Trace("The " + clInbox.DisplayName + " has " + clInbox.ChildFolderCount + " child folders.");

// Now do something with the folder, such as display each child folder's name and ID.
clFoldersFound is a FindFoldersResults
clFolder is a Folder(ServiceEWS)

sFolder is string
clBMimeContent is a MimeContent()
nFile is int

clMail2 is an EmailMessage(ServiceEWS)
bufMsg is Buffer
nLen is int
sFname is string

WHEN EXCEPTION IN
	WHILE bMoreItems = True
		clFoldersFound <- ServiceEWS.FindFolders(WellKnownFolderName.Inbox,Scfilt,vi)				
		bMoreItems = clFoldersFound.MoreAvailable
		IF bMoreItems = True THEN
			vi.Offset = vi.Offset + ipagesize	
		END
		nIIter = 0
		nIDspCnt = clFoldersFound.Folders.Count
		FOR ALL it OF clFoldersFound.Folders 
			clFolder.DisplayName = it.displayname
			sFolder = Upper(clFolder.displayname)
			SWITCH Left(sFolder,3)
				CASE "LON", "HOU", "PER", "NEW", "SGP", "RSA", "BRL"
					Trace(it.DisplayName)	
					clFindresults2 is a 'FindItemsResults<Microsoft.Exchange.WebServices.Data.Item>'
					clFindresults2 <- ServiceEWS.FindItems(it.id,new ItemView(10))					
					Trace("ok")
					
					//Get Items
					FOR ALL i OF clFindresults2.Items
						sFname = "\test_" + TimeSys() + "\.eml"
						xs2 is an 'Collection<Microsoft.Exchange.WebServices.Data.PropertyDefinitionBase>'
						xs2.add(ItemSchema.MimeContent)
						xs2.add(ItemSchema.TextBody)
						clMail2 <- EmailMessage.Bind(ServiceEWS,i.id, new PropertySet(BasePropertySet.IdOnly,xs2))
						fDelete(SysDir(srMyDocuments) + sFname)
						nFile = fOpen(SysDir(srMyDocuments) + sFname,foCreateIfNotExist+foUnicode)
						//Crashes here :(
						fWrite(nFile,clMail2.MimeContent.content)
						fClose(nFile)
						Trace("woop")
					END
					
				OTHER CASE
					Trace("no match - " + it.DisplayName)	
					
			END
			
		END
	END
	
DO
	
END




Viewing all articles
Browse latest Browse all 1914

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>