I have a java code need to change to webMethods java code. Please help

This is for Downloading the all emails attachments we have received

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;

//Requires javax.mail jar file attached to project
public class email {

public static boolean isBlank(final CharSequence cs) {
	int strLen;
	if (cs == null || (strLen = cs.length()) == 0) {
		return true;
	}
	for (int i = 0; i < strLen; i++) {
		if (!Character.isWhitespace(cs.charAt(i))) {
			return false;
		}
	}
	return true;
}

public static void main(String args[]) throws Exception
{
	//Input credentials for GMAIL account
	String emailID="";
	String password="";

	//Properties to access GMAIL IMAP Server.
	Properties properties = new Properties();
	properties.put("mail.imap.host", "imap.gmail.com");
	properties.put("mail.imap.port", "993");
	properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	properties.setProperty("mail.imap.socketFactory.fallback", "false");
	properties.setProperty("mail.imap.socketFactory.port", String.valueOf("993"));


	try {
		//Creating a new Session.
		Session session = Session.getDefaultInstance(properties);
		Store store = session.getStore("imap");
		//Connecting to the Email Server with credentials.
		store.connect("imap.gmail.com", emailID,password);
		//Opening Inbox Folder
		Folder inbox = store.getFolder("INBOX");
		inbox.open(Folder.READ_WRITE);
		int mailNumber = 1;
		//Getting All Messages from Inbox
		Message allMessages[] = inbox.getMessages();
		for(Message eachMessage : allMessages){
			//Getting Each Message From Inbox
			Message msg = eachMessage;
			//Getting Addresses from Last Message
			Address[] in = msg.getFrom();
			for (Address address : in) {
				//Printing from Address
				System.out.println("FROM: " + address.toString());
				System.out.println("Date Recieved: " + msg.getReceivedDate());
			}
			Object content = msg.getContent();
			if(msg.getContent() instanceof String){
				String messageBody = (String)content;
				System.out.println(messageBody);
			}
			else{
				//Reading the message as Multipart
				Multipart multipart = (Multipart) msg.getContent();

				for (int i = 0; i < multipart.getCount(); i++) {
					BodyPart bodyPart = multipart.getBodyPart(i);
					// Dealing with attachments only
					if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) && isBlank(bodyPart.getFileName())) {
						continue; 
					} 
					InputStream is = bodyPart.getInputStream();
					String downloadPath = System.getProperty("user.home") + "/Desktop/";

					System.out.println(downloadPath);
					//Creating new File with Attachment Name on Desktop
					File f = new File(downloadPath + bodyPart.getFileName());
					if(f.exists()){
						f.delete();
						f.createNewFile();
					}
					else{
						f.createNewFile();
					}
					//Writing to new File
					FileOutputStream fos = new FileOutputStream(f);
					byte[] buf = new byte[4096];
					int bytesRead;
					while((bytesRead = is.read(buf))!=-1) {
						fos.write(buf, 0, bytesRead);
					}
					fos.close();
				}
			}
			System.out.println("=====END OF MAIL "+ mailNumber +" =====");
			mailNumber++;
		}
	}catch (Exception mex) {
		mex.printStackTrace();
	}
}

}
Java code downoads all emails attachments.txt (3.5 KB)

Hi Mohammad,

You should take your java code and create out of it 2 services:

  • isBlank
  • downloadAttachments (this is the main method of your current class)

Afterward, you can use the downloadAttachments service from wherever you want (Flow or Java).

If this works, you can do some refactoring work.
For example, you might find another standard service that verifies if a string is blank, so you do not have to do it again.

Hope it helps,
Vlad Turian

Hi,

there are Built-In-Services for the file handling available (when using a recent version of wM 9.x or wM 10.x).

Regards,
Holger