Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

receiving mail with attachments in vb - easiest way 1

Status
Not open for further replies.

HotMadras

Programmer
Apr 20, 2001
74
GB
I'm trying to cobble together an app which will run in the background and periodically check an email account for an email from a specific address, which will contain an attachment. The idea is that it'll just save the attachment from that one email to a file where another application can pick it up and use it.

I'm having difficulty finding examples of how to a) find the correct email without trawling through every email in the account (is this even possible?) and b) extract the attachment from the email once I've found it. I have a base64 decoder routine which will handle that part, but I'm not sure how to proceed with the rest of it.
 
Which e-mail program is receiving the messages?

If Outlook 2000 or later, write a COM Addin (ActiveX DLL) that runs in the Outlook thread and can exploit the Outlook object model. The NewMail event of the application object would be a good starting point.

Try for code examples and other resources.

Paul Bent
Northwind IT Systems
 
Sorry, I don't think I made this clear, this application has to query a pop3 server _directly_. There shouldn't need to be any mail software set up on the machine at all. The idea is that this application runs completely unseen and automatically and just dumps the attachments into a directory for use by other applications.
 
POP-3 commands (covered by RFC1939) are pretty limited.

You can logon to the POP-3 server with a Winsock control ( usually TCP port 110) then use SendData("STAT") and parse the number of e-mails from the return string.

Once the number of e-mails is known, a For...Next block can retrieve the headers with SendData("TOP " & Cstr(i)) where i is the loop counter. The sender is preceded with From: and can be parsed from the header string.

SendData("RETR " & Cstr(i)) will retrieve an e-mail from the mailbox.

That's about all I've done with POP-3 but it shouldn't be too hard to find the filename and the attached file then write it to disk.

Paul Bent
Northwind IT Systems
 
Ok, I knew most of that already, but I hadn't thought of using TOP to minimise the amount of unnecessary downloading/parsing. That will at least solve the problem of how to pick out the right message.

I think extracting the attachment is going to have to be done by parsing the entire message (starting at the end of the headers) for delimiting strings and then base64 decoding the strings in between. I was hoping for a quicker, easier way, but I guess I'll just have to get on with it :)

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top