cancel
Showing results for 
Search instead for 
Did you mean: 

convert java class method

Former Member
0 Kudos

Hi experts,

I have a java class method by which i am fetching mail with attachment and all other parts of a mail but through this class i am getting a string .I want to read mail contents in saprate fields as subject in subject field , mail text in text field , attachment in attachment field etc. Please help me to convert this method according to my requirement. i am using this method in a ABAP report.Also suggest me any predefined FM or method for using java method in ABAP.

Thanks in advance.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

paste your current java code and sample output here.

Former Member
0 Kudos

Hi this is my sample java code

code of FetchMailUsage:-

package com;

import java.io.IOException;

import java.io.InputStream;

import java.util.Date;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.BodyPart;

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Session;

import javax.mail.Store;

import javax.mail.search.SubjectTerm;

public class FetchMailUsage {

public static void main(String[] args) {

// SUBSTITUTE YOUR ISP's POP3 SERVER HERE!!!

String host = Pop3Config.POP3_IP;

// SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!

String username = Pop3Config.USER_NAME;

String password = Pop3Config.PASSWORD;

// SUBSTITUTE YOUR SUBJECT SUBSTRING TO SEARCH HERE!!!

String subjectSubstringToSearch = "Test E-Mail through Java";

// Get a session. Use a blank Properties object.

Session session = Session.getInstance(new Properties());

// session.setDebug(true);

try {

// Get a Store object

Store store = session.getStore("pop3");

store.connect(host, username, password);

// Get "INBOX"

Folder fldr = store.getFolder("INBOX");

fldr.open(Folder.READ_WRITE);

int count = fldr.getMessageCount();

System.out.println(count + " total messages");

// Message numebers start at 1

for(int i = 1; i <= count; i++) {

// Get a message by its sequence number

Message m = fldr.getMessage(i);

// Get some headers

Date date = m.getSentDate();

Address [] from = m.getFrom();

String subj = m.getSubject();

String mimeType = m.getContentType();

System.out.println(date + "\t" + from[0] + "\t" +

subj + "\t" + mimeType);

}

// Search for e-mails by some subject substring

String pattern = subjectSubstringToSearch;

SubjectTerm st = new SubjectTerm(pattern);

// Get some message references

Message [] found = fldr.search(st);

System.out.println(found.length +

" messages matched Subject pattern \"" +

pattern + "\"");

for (int i = 0; i < found.length; i++) {

Message m = found<i>;

// Get some headers

Date date = m.getSentDate();

Address [] from = m.getFrom();

String subj = m.getSubject();

String mimeType = m.getContentType();

System.out.println(date + "\t" + from[0] + "\t" +

subj + "\t" + mimeType);

Object o = m.getContent();

if (o instanceof String) {

System.out.println("*This is a String Message*");

System.out.println((String)o);

}

else if (o instanceof Multipart) {

System.out.print("**This is a Multipart Message. ");

Multipart mp = (Multipart)o;

int count3 = mp.getCount();

System.out.println("It has " + count3 +

" BodyParts in it**");

for (int j = 0; j < count3; j++) {

// Part are numbered starting at 0

BodyPart b = mp.getBodyPart(j);

String mimeType2 = b.getContentType();

System.out.println( "BodyPart " + (j + 1) +

" is of MimeType " + mimeType);

Object o2 = b.getContent();

if (o2 instanceof String) {

System.out.println("*This is a String BodyPart*");

System.out.println((String)o2);

}

else if (o2 instanceof Multipart) {

System.out.print(

"**This BodyPart is a nested Multipart. ");

Multipart mp2 = (Multipart)o2;

int count2 = mp2.getCount();

System.out.println("It has " + count2 +

"further BodyParts in it**");

}

else if (o2 instanceof InputStream) {

System.out.println(

"*This is an InputStream BodyPart*");

}

} //End of for

}

else if (o instanceof InputStream) {

System.out.println("*This is an InputStream message*");

InputStream is = (InputStream)o;

// Assumes character content (not binary images)

int c;

while ((c = is.read()) != -1) {

System.out.write(c);

}

}

// Uncomment to set "delete" flag on the message

//m.setFlag(Flags.Flag.DELETED,true);

} //End of for

// "true" actually deletes flagged messages from folder

fldr.close(true);

store.close();

}

catch (MessagingException mex) {

// Prints all nested (chained) exceptions as well

mex.printStackTrace();

}

catch (IOException ioex) {

ioex.printStackTrace();

}

}

} //End of class

Code of POP3config:-

package com;

import java.util.Properties;

import java.io.FileInputStream;

import java.io.File;

/**

  • @author manoj.tyagi

*/

public class Pop3Config {

protected static String RESOURCE_NAME = "config.txt";

public static String POP3_IP = null;

public static String USER_NAME = null;

public static String PASSWORD = null;

private static Properties prop = new Properties();

public Pop3Config() {

}

static {

try {

loadProperties();

} catch (Exception e) {

System.out.println("Error: " + e);

}

}

public static void loadProperties() throws Exception {

try {

FileInputStream fis = new FileInputStream(new File("c:/config.txt"));

prop.load(fis);

//scheduler messages

POP3_IP = prop.getProperty("pop3.server.ip");

USER_NAME = prop.getProperty("mail.username");

PASSWORD = prop.getProperty("mail.password");

} catch (Exception e1) {

System.out.println("ERROR WHILE LOADING PROPERTY FILE:"

+ RESOURCE_NAME + e1);

throw new Exception("Error while loading property file :"

+ RESOURCE_NAME + " .");

}

}

}

Configuration file:-

#Give the configration of your pop3 server and mail.username and mail.password

pop3.server.ip= 192.168.1.35

mail.username= XXXXXXXXXXXX

mail.password= XXXXXXXXXXXXX

My ABAP table is:-

SENDER_ID NUMC 6

RECIEVER BBP_BAPI_AD_SMTPADR CHAR 241

SENDER BBP_BAPI_AD_SMTPADR CHAR 241

SUBJECT CHAR 60

ATTACHEMENT ZMAIL_TYPE LRAW 6000

Former Member
0 Kudos

You can move the code in main method to a method which would return you an array or say vector of <b>Message</b> objects like this.


public Vector getMessages()

or

public Message[] getMessages()

and in the code which will give a call to this method you can fetch this relevent information for each message in the list.

say if you call it inside main,

public static void main(String []args)
{
....
Messages allMessages[] = getMessages();

for(int cnt=0;cnt<allMessages.length;cnt++)
{
String fldName = allMessages[cnt].getXXX();
}

....
}

Hope this helps.

Former Member
0 Kudos

hi,

can u convert it accordingly