Hi,
I am new to VB.NET and have written a simple application which accesses Outlook and saves email attachments to a folder, before deleting the emails.
This is requested from a scheduled task, it all works when I'm logged in as the user ( Called mailuser ).
Ideally I don't want to leave a session logged in, but when I try the application without a mailuser session logged in it doesn't seem to run to completion. It never saves any attachments and sometimes fails to close the application and Outlook.
The application is running under the correct user ( mailuser ), which is specified in the scheduled task.
Please could anybody explain how to set up the application to run without having to leave a session logged in ?
The code for the application is shown below :
Thanks
Robin
I am new to VB.NET and have written a simple application which accesses Outlook and saves email attachments to a folder, before deleting the emails.
This is requested from a scheduled task, it all works when I'm logged in as the user ( Called mailuser ).
Ideally I don't want to leave a session logged in, but when I try the application without a mailuser session logged in it doesn't seem to run to completion. It never saves any attachments and sometimes fails to close the application and Outlook.
The application is running under the correct user ( mailuser ), which is specified in the scheduled task.
Please could anybody explain how to set up the application to run without having to leave a session logged in ?
The code for the application is shown below :
VB.NET:
Module Module1
Sub Main()
Dim objOL As Microsoft.Office.Interop.Outlook.Application
Dim objNS As Microsoft.Office.Interop.Outlook.NameSpace
Dim myItems As Microsoft.Office.Interop.Outlook.Items
Dim x As Int16
Dim styloEnv As New Stylo.Common.EnvironmentSettings
Dim logFile As New Stylo.Common.LogFile
Dim logName As String
logName = logFile.GetNewLogFileName("MailUserExtractAttachments_", ".txt")
logFile.WriteToLog(logName, Now.ToString & " - Start.")
Try
logFile.WriteToLog(logName, Now.ToString & " - Starting to get Outlook info")
objOL = New Microsoft.Office.Interop.Outlook.Application()
objNS = objOL.GetNamespace("MAPI")
Dim olfolder As Microsoft.Office.Interop.Outlook.MAPIFolder
Dim Atmt As Microsoft.Office.Interop.Outlook.Attachment
logFile.WriteToLog(logName, Now.ToString & " - Attempting to log on to Outlook")
objOL.GetNamespace("MAPI").Logon()
logFile.WriteToLog(logName, Now.ToString & " - Logged on to Outlook")
logFile.WriteToLog(logName, Now.ToString & " - Get Outlook Default Folder")
olfolder = objOL.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
logFile.WriteToLog(logName, Now.ToString & " - Found default folder")
myItems = olfolder.Items
Dim y = myItems.Count
x = 1
logFile.WriteToLog(logName, Now.ToString & " - No Items - " & y)
While x <= y
logFile.WriteToLog(logName, Now.ToString & " - Found email from - " & myItems.Item(x).SenderEmailAddress)
logFile.WriteToLog(logName, Now.ToString & " - Email contains - " & myItems.Item(x).Subject)
For Each Atmt In myItems.Item(x).attachments
Dim currDate As Date
Dim formattedStamp As String
currDate = Now
formattedStamp = currDate.Year.ToString("0000") & _
currDate.Month.ToString("00") & _
currDate.Day.ToString("00") & _
"_" & _
currDate.Hour.ToString("00") & _
currDate.Minute.ToString("00") & _
currDate.Second.ToString("00") & _
currDate.Millisecond.ToString("000")
Dim filename = "G:\MailUserAttachments\" + Atmt.FileName + "_" + formattedStamp
Atmt.SaveAsFile(filename)
logFile.WriteToLog(logName, Now.ToString & " - Saving attachment - " & Atmt.FileName)
logFile.WriteToLog(logName, Now.ToString & " - As - " & filename)
Next Atmt
myItems(x).delete()
olfolder = objOL.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
myItems = olfolder.Items
y = myItems.Count
x = 1
objOL.GetNamespace("MAPI").Logoff()
End While
Catch ex As Exception
logFile.WriteToLog(logName, Now.ToString & " - " & ex.Message)
logFile.WriteToLog(logName, Now.ToString & " - Aborting.")
Environment.Exit(1)
End Try
End Sub
End Module
Thanks
Robin