cfimap - download email as an attachment

Posted on 2014-01-30
Last Modified: 2014-04-07

I'm using cfimap to store the contents of emails in our DB as well as to download documents attached to the email.  Our users are requesting to have the actual email downloaded as an attachment as well for reference. Can this be done?

Question by:HumanScaleDev
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 52

Accepted Solution

_agx_ earned 500 total points
ID: 39821520
It can be done, yes. But not with CFIMAP. It doesn't expose the raw message, only the "parsed" message parts.  

If you need the raw message, you'll have to use a low level java library to connect to the mail server, grab the raw messages, and save them as binary.  Not a simple task.
LVL 52

Expert Comment

ID: 39829809
HumanScaleDev - Can I ask why the "B" grade? I know it is not the answer you wanted to hear, but unfortunately "not possible with cfimap" is the correct answer.

(I'd be happy to explore the java approach, but given its greater complexity, you didn't say whether or not that was an option here)

Author Comment

ID: 39892359
Where can I see the grade of this question? Sorry I was not familiar with the way EE handles grades. I was assuming 500 points was the rate reflected on the person that answers the question and the grade on the question itself. Meaning, if I would be looking into this and found this question, I'd loved to find an example on how this could be achieved. Anyway, if grades and points are not tied together and they reflect on the person that answer the question, then ok, I'd give _agx_ and A.
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

LVL 52

Expert Comment

ID: 39894657
I'm not that familiar with where they're shown when asking a question.  I *think* they're visible when you close the question, maybe in your profile history too?

> I'd loved to find an example on how this could be achieved.

Sure.  I'm on a deadline today, but I'll put up an example this w/e.  In summary - unfortunately given that cfimap doesn't support it you have two choices:

1) process everything (ie email details: sender, date, subject, ...) in java -OR-
2) use CFIMAP to process the details and use java to get a copy of the whole message

Though it's a bit of double work, I'd go with option #2 because it's much simpler. Then you don't have to write a ton of extra code to do the rest, since CFMIMAP does that already. The only thing it doesn't give you is a copy of the full message.
LVL 52

Expert Comment

ID: 39909896
I haven't forgotten about this, I'm just swamped with work since last week.

Author Comment

ID: 39910726
Don't worry about it. I'm sure if someone else needs it they'll post a question about it :). We're good. Thanks!
LVL 52

Expert Comment

ID: 39931958
Finally had a chance to get back to this! (Sorry for the delay) Like I mentioned earlier, there's 2 approaches. The simpler one is use java only to get and save the message, and keep using cfimap for everything else.

To save the message, you 1st need to connect to the mail server. Obviously different mail servers have different settings (port number, folders, ssl, tls, etc...) so the actual code may vary. Here is an translated example for gmail that I know works w/CF10:

	// initialize mail session props
	props = createObject("java", "java.util.Properties").init();
	props.setProperty("", "imaps");
	// session is reserved word in CF
	JavaXSession = createObject("java", "javax.mail.Session");
	mailSession = JavaxSession.getDefaultInstance(props, javacast("null", ""));
	mailStore = mailSession.getStore("imaps");
	mailStore.connect("", "", "somePassword");
	mailFolder = mailStore.getFolder("Inbox");;

        // .... code to retrieve and save messages

       // close when finished

Open in new window

Once you've successfully connected, check the number of messages with mailFolder.getMessageCount().  Then use a loop to save each message to a file.  Obviously you don't want to try and save a gazillion messages at once... the page will probably time out.  Here's how you could do it in small batches:

      //... connect to mail server

       // TEST: save maximum of 5 messages only
	maxMessages = 0;
	if (mailFolder.getMessageCount() > 0) {
		maxMessages = Min(mailFolder.getMessageCount(), 5);

	FileOutputStream = createObject("java", "");	
	// save a maximum of 5 messages ....
	for (i = 1; i < maxMessages; i++) {
		// get current email
		message = mailFolder.getMessage(i);	
		// generate a unique file name 
		saveToFile = expandPath("./"& createUUID() &".eml");
		// save the email to file
		outStream = FileOutputStream.init(saveToFile);
		WriteOutput("<br>[#i#] saveToFile = #saveToFile#");

	// always close when finished

Open in new window

That is a greatly simplified example, but hopefully should get you started.

Author Comment

ID: 39984183
thank you!

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them ( . I did keep the main js functions but made sever…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 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