Writing the Email attachments to a destination folder

Hi Folks,

I have the following code to get the attachments form Email, now I need to write the attachments to a detination on my desktop. How should I apply the outputstream to write the attachment files to the destination?

public class ReadMailAttachmentAndAppendToFile extends AbstractTransformation {

	@Override
	public void transform(TransformationInput in, TransformationOutput out)
			throws StreamTransformationException {

		String contentType = "";

		try {

			getTrace().addInfo("java map started");

			// Get the all of the attachments
			InputAttachments inputAttachments = in.getInputAttachments();
			OutputAttachments outputAttachments = out.getOutputAttachments();

			Collection<String> ContentIDs = inputAttachments
					.getAllContentIds(true);
			Object[] arrayObj = ContentIDs.toArray();
		//	getTrace().addDebugMessage("Array object initialized ");
			int attachmentCount = arrayObj.length;

			// Check if there are attachments.
			if (attachmentCount > 0) {
				// Grab the contentIDs that identify each attachment.

			//	int xlsCount = 0;
				getTrace().addInfo(
						"*** Attachments count: " + attachmentCount);

				for (int a = 0; a < attachmentCount; a++) {
					String contentID = (String) arrayObj[a];

					// Grab the individual attachment by its contentID
					Attachment attachment = inputAttachments
							.getAttachment(contentID);

					// Get the contentType which contains the filename.
					contentType = attachment.getContentType().toString();

					// Parse the contentType string to get the filename.
					contentType = contentType.replaceAll("\"", "");
					int x = contentType.lastIndexOf("=") + 1;
					String filename = contentType.substring(x);
					getTrace().addInfo("*** Filename: " + filename);

				//	xlsCount++;

					// Report

				/*	if (!filename.replaceAll(" ", "").toUpperCase()
							.contains("XXXXX".toUpperCase())) {

						outputAttachments.removeAttachment(contentID);

					} */


					getTrace().addInfo("*** Parsing complete.");

				}
			}

		} catch (Exception e) {

			// Send notification letting the sender know of errors.
			String statusMessage = "Error in processing ";
			throw new StreamTransformationException(statusMessage
					+ e.getMessage());
		}
	}

}

Open in new window

kalyangkmAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Well that's a tricky one as there's no class called 'Attachment' in the standard Java APIs. What package does that belong to?
0
kalyangkmAuthor Commented:
Hi CEHJ,

I should have mentioned this before. This is an SAP provided class. The package is com.sap.aii.mapping.api.Attachment

And the details are below
https://help.sap.com/doc/javadocs_pi_pi711/7.1.1.2/en-US/com/sap/aii/mapping/api/class-use/Attachment.html
0
CEHJCommented:
That should be OK. There appears to be no way of getting a 'file name' from that, so what algo do you want to use to give it one?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

kalyangkmAuthor Commented:
I am already getting the file name if you see my code.
0
CEHJCommented:
I'm suprised that will give you the file name. The docs say nothing other than stuff about mimetypes
0
CEHJCommented:
OK but it doesn't say much about file names. Anyway are you actually getting satisfactory names?
It might help me to give you the right code if you could let me see some (non-sensitive) actual pairs of getContentType and getContentId
0
kalyangkmAuthor Commented:
Yes, I got the filename here is the code and also I am able to use the Output

	// Get the all of the attachments
			InputAttachments inputAttachments = in.getInputAttachments();
			//OutputAttachments outputAttachments = out.getOutputAttachments();

			Collection<String> ContentIDs = inputAttachments
					.getAllContentIds(true);
			Object[] arrayObj = ContentIDs.toArray();
		//	getTrace().addDebugMessage("Array object initialized ");
			int attachmentCount = arrayObj.length;

			// Check if there are attachments.
			if (attachmentCount > 0) {
				// Grab the contentIDs that identify each attachment.

			
				getTrace().addInfo(
						"*** Attachments count: " + attachmentCount);

				for (int a = 0; a < attachmentCount; a++) {
					String contentID = (String) arrayObj[a];

					// Grab the individual attachment by its contentID
					Attachment attachment = inputAttachments
							.getAttachment(contentID);

					// Get the contentType which contains the filename.
					contentType = attachment.getContentType().toString();

					// Parse the contentType string to get the filename.
					contentType = contentType.replaceAll("\"", "");
					int x = contentType.lastIndexOf("=") + 1;
					String filename = contentType.substring(x);
					getTrace().addInfo("*** Filename: " + filename);

Open in new window



And also I am able to get the file output stream working

byte[] attachmentBytes = attachment.getContent();					
					File path = new File("//XXXXXX/"+YYYYY+"/TTTTT/RRRRR/NNNNN/"+filename);					
					FileOutputStream fos = new FileOutputStream(path);
					fos.write(attachmentBytes);	

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
That's good. Very strange that they have bundled the file name into the content type without providing a separate method. Also be aware that write(byteArray) might not write the full array owing to buffering. I'd probably use DataOutputStream.writeFully
0
kalyangkmAuthor Commented:
Sure, I will make a note of it. Thank You.
0
kalyangkmAuthor Commented:
I was able to get the solution on my own. I am not interested in assigning Best Solution to myself but as I wanted to close the question I have done that. Anyway if ts an issue you can change the Best Solution to CEHJ and close the question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.