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?
 
kalyangkmConnect With a Mentor Author 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
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.