The following VB codes demonstrates how to import RFC822 message files to Outlook INBOX/PST file. It requires Outlook is installed on current machine.
Sections:
Before you can use the following sample codes, you should download the EAGetMail Installer and install it on your machine at first. Full sample projects are included in this installer.
To use EAGetMail POP3 & IMAP4 ActiveX Object in your project,
the first step is Add reference of EAGetMail to your project
.
Please go to menu
-> Project
-> References
-> and select EAGetMailObj ActiveX Object
, click OK
,
the reference will be added to your project, and you can start to use it to retrieve email and parse email in your project.
Open VBA IDE by press Alt+F11
, Please select menu -> Tools
-> References
-> and select EASendMailObj ActiveX Object
,
click OK, the reference will be added to current VBA project, and you can start to use it to retrieve email and parse email in your VBA project.
To better demonstrate how to retrieve email and parse email, let’s create a VB 6.0 Standard EXE project at first, then add a CommandButton on the Form, double-click this button. It is like this
The following example codes demonstrate importing emails from Outlook PST file to RFC822 message files.
Note
To get full sample projects, please download and install EAGetMail on your machine.
' Import rfc822 email file to Outlook Inbox folder or PST file.
Sub ImportSingleRfc822Message(ByVal fileName)
Dim oTool
Set oTool = CreateObject("EAGetMailObj.Tools")
Dim app
Set app = CreateObject("Outlook.Application")
Dim mail
Set mail = CreateObject("EAGetMailObj.Mail")
mail.LicenseCode = "TryIt"
Dim rfc822File
rfc822File = fileName
mail.LoadFile rfc822File, False
Dim tempMsgFile
tempMsgFile = rfc822File & ".msg"
mail.SaveAsOMSG tempMsgFile, True, False
Const olFolderInbox = 6
Const olDiscard = 1
Dim folder
Set folder = app.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
' If you want to create a new PST file
' or existed PST file to store the imported
' message, you can use the following codes.
' app.Session.AddStore("d:\tempFolder\test.pst")
' Set folder = app.GetNamespace("MAPI").Folders.GetLast()
Dim item
Set item = app.Session.OpenSharedItem(tempMsgFile)
item.Save
item.Move folder
item.Close olDiscard
app.Quit
oTool.RemoveFile tempMsgFile
End Sub
' Import rfc822 email files to Outlook Inbox folder or PST file.
Sub ImportRfc822Message(ByRef files)
Dim oTool
Set oTool = CreateObject("EAGetMailObj.Tools")
Dim app
Set app = CreateObject("Outlook.Application")
Const olFolderInbox = 6
Const olDiscard = 1
Dim folder
Set folder = app.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
' If you want to create a new PST file
' or existed PST file to store the imported
' message, you can use the following codes.
app.Session.AddStore("d:\tempFolder\test.pst")
Set folder = app.GetNamespace("MAPI").Folders.GetLast()
Dim i
For i = LBound(files) To UBound(files)
Dim mail
Set mail = CreateObject("EAGetMailObj.Mail")
mail.LicenseCode = "TryIt"
Dim rfc822File
rfc822File = files(i)
mail.LoadFile rfc822File, False
Dim tempMsgFile
tempMsgFile = rfc822File & ".msg"
mail.SaveAsOMSG tempMsgFile, True, False
Dim item
Set item = app.Session.OpenSharedItem(tempMsgFile)
item.Save
item.Move folder
item.Close olDiscard
oTool.RemoveFile tempMsgFile
Next
app.Quit
End Sub
Private Sub Command1_Click()
On Error GoTo ErrorHandle
Dim files
Dim oTool
Set oTool = CreateObject("EAGetMailObj.Tools")
files = oTool.GetFiles("D:\sourcefolder\*.eml")
ImportRfc822Message files
' You can use the following code to import a single rfc822 message file.
' ImportSingleRfc822Message "d:\sourcefolder\test.eml"
Exit Sub
ErrorHandle:
MsgBox Err.Description
End Sub
Seperate builds of run-time dll for 32 and x64 platform
File | Platform |
Installation Path\Lib\native\x86\EAGetMailObj.dll | 32 bit |
Installation Path\Lib\native\x64\EAGetMailObj.dll | 64 bit |
Standard EXE
For VB6, C++, Delphi or other standard exe application, you can distribute EAGetMailObj.dll with your application to target machine without COM-registration and installer. To learn more detail, please have a look at Registration-free COM with Manifest File.
Script
For ASP, VBScript, VBA, MS SQL Stored Procedure, you need to install EAGetMail on target machine by EAGetMail installer, both 32bit/x64 DLL are installed and registered.
Appendix
Comments
If you have any comments or questions about above example codes, please click here to add your comments.