Solved

fetch email attachments

Posted on 2009-05-12
15
455 Views
Last Modified: 2012-05-06
is it possible to extract just the attachment out of a email

i have created imap account email user test@ wwwwww.net

which will get two emails daily the emails will only have a subject dbstats1 or dbstat2 and a zip attachment

would like in vbs or python
0
Comment
Question by:James Murrell
15 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24363815
I know you would prefer vbs or python but here is a library you can use from a .Net application: http://www.lesnikowski.com/mail/Index.aspx
0
 
LVL 31

Author Comment

by:James Murrell
ID: 24363944
no to .net now development tools / not enough learning time
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24363971
Hi, cs97jjm3.

What email client do you have on your computer?  If it's Outlook, then I can show you how to script this using VBScript.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 31

Author Comment

by:James Murrell
ID: 24363993
thunderbird... but could setup outlook, but ideally not.....

0
 
LVL 76

Expert Comment

by:David Lee
ID: 24364047
Sorry, don't know anything about Thunderbird other than what it is.  If you decide to use Outlook, then I'll be glad to help.
0
 
LVL 31

Author Comment

by:James Murrell
ID: 24364123
Outlook express ok?

0
 
LVL 76

Expert Comment

by:David Lee
ID: 24364143
Outlook Express isn't programmable.  Only the full version of Outlook.
0
 
LVL 31

Author Comment

by:James Murrell
ID: 24369458
ok can use outlook if need be? 2003 ok

0
 
LVL 76

Expert Comment

by:David Lee
ID: 24369976
Outlook 2003 is fine.  Here's the code.  Follow these instructions to use this.

1.  Open Notepad
2.  Copy the code in the snippet  
3.  Paste the code into Notepad
4.  Edit the code as needed.  I placed comments where things can or should change.
5.  Save the file with a .vbs extension
6.  Run the code by double-clicking on the file

The code uses Outlook to check all messages in the inbox.  If a message has a subject of "dbstats1" or "dbstats2", then the code saves all the message attachments to the folder of your choice.
Dim olkApp, olkSes, olkFld, olkMsg, olkAtt
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNameSpace("MAPI")
'Change the profile name on the next line'
olkSes.Logon "Outlook"
Set olkFld = olkSes.GetDefaultFolder(6)
For Each olkMsg in olkFld.Items
    Select Case LCase(olkMsg.Subject)
        Case "dbstats1", "dbstats2"
            For Each olkAtt in olkMsg.Attachments
                'Change the path on the next line as desired'
                olkAtt.SaveAsFile "C:\Some Folder\" & olkAtt.Filename
            Next
    End Select
Next
Set olkAtt = Nothing
Set olkMsg = Nothing
Set olkFld = Nothing
olkSes.Logoff
Set olkSes = Nothing
Set olkApp = Nothing

Open in new window

0
 
LVL 31

Author Comment

by:James Murrell
ID: 24371989
silly question, does outlook have to be open? if so can code do that... and do a send and recieve
0
 
LVL 31

Author Comment

by:James Murrell
ID: 24372035
ok ignore last comment

Set oShell = CreateObject("WScript.Shell")
oShell.Run "outlook"

did the trick -


but can code mark to delete? or delete

ps: cheers for this
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 24372980
"does outlook have to be open?"
No, the code launches Outlook in the background.  Your solution will work too.

"and do a send and recieve"
No.  Your solution does that though.

"can code mark to delete? or delete"
Yes, the code can delete the item.  I revised the code to delete the item after processing.
Dim olkApp, olkSes, olkFld, olkMsg, olkAtt, intIndex
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNameSpace("MAPI")
'Change the profile name on the next line'
olkSes.Logon "Outlook"
Set olkFld = olkSes.GetDefaultFolder(6)
For intIndex = olkFld.Items.Count to 1 Step -1
    Set olkMsg = olkFld.Items.Item(intIndex)
    Select Case LCase(olkMsg.Subject)
        Case "dbstats1", "dbstats2"
            For Each olkAtt in olkMsg.Attachments
                'Change the path on the next line as desired'
                olkAtt.SaveAsFile "C:\Some Folder\" & olkAtt.Filename
            Next
            olkMsg.Delete
    End Select
Next
Set olkAtt = Nothing
Set olkMsg = Nothing
Set olkFld = Nothing
olkSes.Logoff
Set olkSes = Nothing
Set olkApp = Nothing

Open in new window

0
 
LVL 31

Author Closing Comment

by:James Murrell
ID: 31580490
Thanks you for you help, you explain the code very well a star a ee i would say
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24373028
Thank you!  Glad I could help out.
0
 

Expert Comment

by:alam747
ID: 35055173
Please let me know how to filter only the attached of daily data like todays data not all of the attached email.  Thanks for your co-operation and understanding.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question