Using Outlook rules wizard and VB.NET how would I extract an attachment from an email? Once I have the file I will be importing it into SQL Server or Access.
Printable View
Using Outlook rules wizard and VB.NET how would I extract an attachment from an email? Once I have the file I will be importing it into SQL Server or Access.
Texas,
Did you find a solution for this problem?
I am wanting to do the same thing.
I know how to connect to the mailbox and I know how to check if the email has an attachment, just don't know how to display it/link to it and or save it or import it to my db?
See - http://www.vbforums.com/showthread.php?t=352571
I would be very grateful for any advise
Cheers Al
no idea to be honest.
i think i ended up having someone or something drop the file on the network then i imported it into sql.
Outlook code is almost the same in VB6 and VB.NET. I have allot of code examples on the Forums but here is one that should show you the mechanics and logic involved. Just customize to your needs. ;)
Outlook Attachments
Gangsta Y,
Thanks very much, I hope you don't mind if I ask a few questions.
I am looking to get the messages directly out of exchange and want to pass in different mailbox ID depending on which group the user who's logged in belongs to. Is this possible?
So it may be a sales mailbox or a marketing mailbox. Was hoping I could do something like -
VB Code:
'Set oNS = oApp.GetNamespace("MAPI") Set oNS = oApp.GetNamespace("MAPILEVEL=Mailbox - Joe Bloggs")
I've managed to get your sample working, have commented out these lines, was getting a permissions error, plus I don't understand what they're supposed to be doing?
so am just saving the attachment using -VB Code:
Set oMsg = oApp.CreateItemFromTemplate("C:\Item[" & i & "].msg") oMsg.Attachments.Item(1).SaveAsFile ("C:\Item[" & i & "].log") Kill "C:\Item[" & i & "].msg"
VB Code:
oAttach.SaveAsFile ("C:\" & oAttach.FileName)
On the side: Once I get this thing working in .Net I'll be moving it to ASP.NET
Cheers Al
Ok, here is how you can logon to opther mailboxes if you have permissions.
You dont need the CreateItemFromTemplate because it is just opening a saved message from the filesystem. The SaveAsFile you should provide a complete path or it will save to the temp dir. ;)VB Code:
Option Explicit 'Add a reference to Outlook xx.0 object library Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim oApp As Outlook.Application Dim oNS As Outlook.Namespace oApp = New Outlook.Application oNS = oApp.GetNamespace("MAPI") oNS.Logon "ProfileName", "Password", "ShowDialog", "NewSession" End Sub
RobDog,
Thanks a whole bunch for that, do you know whether this will work if it's running on a web server with no one logged in?
I have a horrible feeling it won't... although with the method of connection that you've described above (passing in the username & pwd) it could work??
This is what I have so far, what do you think -
VB Code:
Private Sub btnSaveAttach_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAttach.Click Dim oApp As New Outlook.Application Dim oNS As Outlook.NameSpace Dim oFolder As Outlook.MAPIFolder Dim oEmail As Outlook.MailItem Dim oAttachs As Outlook.Attachments Dim oAttach As Outlook.Attachment Dim i As Integer oNS = oApp.GetNamespace("MAPI") oFolder = GetFolder("Editorial\Inbox") For Each oEmail In oFolder.Items MessageBox.Show(oEmail.Subject) oAttachs = oEmail.Attachments For i = 1 To oAttachs.Count oAttach = oAttachs.Item(i) MessageBox.Show(oAttach.DisplayName & " " & oAttach.FileName) If oAttach.FileName = "Extensions.doc" Then oAttach.SaveAsFile("C:\" & oAttach.FileName) End If oAttach = Nothing Next Next oEmail oAttachs = Nothing oEmail = Nothing oFolder = Nothing oNS = Nothing ' Exit Outlook - shuts it down 'oApp.Quit oApp = Nothing End Sub Public Function GetFolder(ByVal sFolderPath As String) As MAPIFolder ' folder path needs to be something like ' "Public Folders\All Public Folders\Company\Sales" ' source - [url]http://www.outlookcode.com/d/code/getfolder.htm[/url] Dim objApp As Outlook.Application Dim objNS As Outlook.NameSpace Dim colFolders As Outlook.Folders Dim objFolder As Outlook.MAPIFolder Dim arrFolders() As String Dim i As Long 'On Error Resume Next sFolderPath = Replace(sFolderPath, "/", "\") arrFolders = Split(sFolderPath, "\") objApp = CreateObject("Outlook.Application") objNS = objApp.GetNamespace("MAPI") objFolder = objNS.Folders.Item(arrFolders(0)) If Not objFolder Is Nothing Then For i = 1 To UBound(arrFolders) colFolders = objFolder.Folders objFolder = Nothing objFolder = colFolders.Item(arrFolders(i)) If objFolder Is Nothing Then Exit For End If Next End If GetFolder = objFolder colFolders = Nothing objNS = Nothing objApp = Nothing End Function
I dont believe it will run if the system is locked. What you would be better off doing is writting a web service or plain service. ;)
Right looks like I'll have to look into the web service thing then
This code works great, albeit a bit slow, but I can't work out how to get the attachments, there is a flag in the returned dataset which marks whether there is an attachment or not. How to get them that's the question?
VB Code:
Private Sub btnOpenInbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenInbox.Click Dim ds As New DataSet Dim dt As DataTable Try 'SELECT Subject, Contents FROM Inbox Dim da As New OleDbDataAdapter("SELECT * FROM Inbox", _ "Provider=Microsoft.Jet.OLEDB.4.0;Outlook 9.0;" & _ "MAPILEVEL=Mailbox - Jo Smith|;PROFILE=MS Exchange Settings;" & _ "TABLETYPE=0;DATABASE=C:\WINNT\Temp") da.Fill(ds, "Outlook") dt = ds.Tables("Outlook") DataGrid1.DataSource = dt Catch exc As OleDbException Dim OleDBError1 As OleDbError For Each OleDBError1 In exc.Errors Console.WriteLine(OleDBError1.Message) Next End Try End Sub
Thanks for all your help
Cheers Al
Nope, not at the folder level. You would need to check each item in the folder for the existance of it in the Attachments collection of the item. ;)