check and move mail from Outlook

powerteh

Member
Joined
Mar 23, 2006
Messages
13
Programming Experience
Beginner
hi all,
is it possible for us to using VB .NET to move all mail from Outlook to a folder(created by ourself) ?

Pls show me the code.
Your information are appreciated.

thanks
 
This article shows how to get all messages, just ignore the 'unread' filter they use ;)

"How to retrieve unread messages from Inbox by using Outlook Object Model in Visual Basic .NET"
http://support.microsoft.com/?kbid=313795
 
hi John,
Thanks for sharing that to me, i really appreciate that!
I have another question here:
i) the example you gave me is to retrieve all mails from Outlook and shown by using console application. However, what i want is to retrieve and save the mails in another folder created by user.
Can it possible to be done by using VB .NET?

thanks and regards,
powerteh
 
console or desktop app doesn't matter, code to outlook is the same.
 
Erm...let me cut it short!
What i want is to move (or copy) all mails from outlook to desktop (created folder).
And i know the example you gave me can retrieve all the mails, but how to copy the mails to desktop?

Thanks for helping.
 
Here is code to get path of a folder (temp) located on desktop:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myPath [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\temp\"
[/SIZE]
For each oMsg As Outlook.MailItem you can save the different info you need like Subject and Body etc in the file and format you wish.

You can also use the SaveAs method of MailItem to save the complete email in one of the available filetypes. http://msdn.microsoft.com/library/en-us/vbaol11/html/olmthSaveAs_HV05247719.asp As documentation says, SaveAs method gives a warning message to make it difficult to exploit.
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim i As Integer

' Create Outlook application.
Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

oNS.Logon("YourValidProfile", Missing.Value,
False, True) ' TODO:
' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim oItems As Outlook.Items = oInbox.Items

If oItems.Count <= 0 Then
MessageBox.Show("There is no any mails in your inbox on :" & Date.Today())
Else
MessageBox.Show("Total numbers of mails : " & oItems.Count & vbNewLine & _
"Are you sure want to move all the MS Outlook Inbox Mails?", "Confirmation", _
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)

If MsgBoxResult.OK Then
Dim myPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\MyOutlookMail\" & Date.Today()

For i = 0 To oItems.Count - 1
oItems.SaveAs("C:\" & myPath & ".msg")
Next
Else
oNS.Logoff()
End If
End If

End Sub


**These are my codes to move all the mails from outlook to the specific folder.
Currently i am using VB .NET 2003,and there is an error message says that :

"An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dll
Additional information: Public member 'SaveAs' on type 'ItemsClass' not found."

What cause this problem occured? (my programming logic is wrong:confused: )
Am i need to import any namespace for this:confused:

Please help for this.

thanks and regards,
powerteh

 
oItems.Item(i).SaveAs
And you might want to give each message it's unique file name.
 
hi John,

Sorry for disturb you again!

i have try the code that you gave me, and make a little changes to it.
However, the error occured as below :
"An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in microsoft.visualbasic.dll
Additional information: The operation failed."

My code are as below:

oItems.Item(i).SaveAs("C:\Documents and Settings\Desktop\MyOutlookInbox" _
&
Date.Today & "(" & i & ")" & ".msg")

I do not know why the operation failed.
Can you help em to figure out?

thanks and regards,
powerteh
 
Hi, I have tried so many times to get total number of unread message in the inbox foloder of Outlook express. Each time I get 0 as result.

I tried with the code given above by powerteh also but this time I am getting error "The server is not available. Contact your administrator if this condition persists." at line oNS.Logon("YourValidProfile", Missing.Value, False, True) even I use username and password properly.

Actually I want to make a scheduler service which will give message whenever any new(unread) message come to inbox.

Please help me regading this as I am beginner for outlook programming.
 
Back
Top