Hello,
I’m using SAP BC and I want to received emails with more than one attachments, the structure transportinfo does not seem to manage to received more than one.
Can you help me. Perhaps I should use the ContentHandler ?
Thank You
Matthieu
Hello,
I’m using SAP BC and I want to received emails with more than one attachments, the structure transportinfo does not seem to manage to received more than one.
Can you help me. Perhaps I should use the ContentHandler ?
Thank You
Matthieu
You could take a look into wmSamples.mime folder. Also ch.19 working with MIME (ISDevelopersGuide).
Cheers
Hello,
The problem is not to extract the information from the email.
The problem is to have all the attachments in the same record(Values) without invoking the service as many times as the number of attachments.
The problem is to catch the stream from the port directly.
Matthieu
Oddly enough I’m having this exact same problem right now.
Is your goal to handle generic emails as opposed to specifically formatted emails?
My approach is going to be to write a service that checks the email inbox and avoid using the email listener. The email listener is designed for formatted service invocations and not random email processing. I could share my service once it is working if you like.
Here is my source code. This is tested to some extent. It assumes that you want to save all attachments that are not text/plain, text/html, or text/xml to a file for processing as a file. It generates a filename for the file based on the date and saves it to /tmp
NOTE: to run this code sample will require the Java Mail .jar file a POP3 or IMAP provider .jar file and the Java Activation Framework .jar file All of these are available for free from Sun. Place these .jar files in the lib directory of your package.
IDataCursor idc = pipeline.getCursor();
String provider = “POP3”;
String mailhost = “”;
String username = “”;
String password = “”;
if(idc.first(“provider”))
provider = (String) idc.getValue();
if(idc.first(“mailhost”))
mailhost = (String) idc.getValue();
if(idc.first(“username”))
username = (String) idc.getValue();
if(idc.first(“password”))
password = (String) idc.getValue();
try
{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props,null);
Store store = session.getStore(provider);
store.connect(mailhost,username,password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
IData inboxData[] = new IData[messages.length];
for(int i = 0; i < messages.length; i++)
{
inboxData[i] = getHeaders(messages[i]);
IDataCursor midc = inboxData[i].getCursor();
Object body = messages[i].getContent();
if (body instanceof Multipart)
{
midc.insertAfter("body", processMultipart((Multipart) body));
}
else
{
IData wrap[] = new IData[1];
wrap[0] = processPart(messages[i]);
midc.insertAfter("body", wrap);
}
}
inbox.close(false);
store.close();
idc.insertAfter("inbox",inboxData);
}
catch(Exception e)
{
}
javax.mail.*
javax.mail.internet.*
java.io.*
java.util.Date
java.util.Properties
java.text.SimpleDateFormat
public static IData getHeaders(Message message)
throws MessagingException
{
IData headers = IDataFactory.create();
IDataCursor hc = headers.getCursor();
InternetAddress fromIA[] = (InternetAddress[])message.getFrom();
if(fromIA != null)
{
String from = fromIA[0].getAddress();
if (from != null)
{
hc.insertAfter("From", from);
}
}
String replyTo = InternetAddress.toString(message.getReplyTo());
if (replyTo != null)
{
hc.insertAfter("Reply-to", replyTo);
}
String to = InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
if (to != null)
{
hc.insertAfter("To", to);
}
String cc = InternetAddress.toString(message.getRecipients(Message.RecipientType.CC));
if (cc != null)
{
hc.insertAfter("Cc", cc);
}
String bcc = InternetAddress.toString(message.getRecipients(Message.RecipientType.BCC));
if (bcc != null)
{
hc.insertAfter("Bcc", to);
}
String subject = message.getSubject();
if (subject != null)
{
hc.insertAfter("Subject", subject);
}
Date sent = message.getSentDate();
if (sent != null)
{
hc.insertAfter("Sent", sent);
}
Date received = message.getReceivedDate();
if (received != null)
{
hc.insertAfter("Received", received);
}
return headers;
}
public static IData processMultipart(Multipart mp)
throws MessagingException
{
IData multi = new IData[mp.getCount()];
for (int i = 0; i < mp.getCount(); i++)
{
multi[i] = processPart(mp.getBodyPart(i));
}
return multi;
}
public static IData processPart(Part p)
throws MessagingException
{
IData part = IDataFactory.create();
IDataCursor partc = part.getCursor();
try
{
String fileName = p.getFileName();
String disposition = p.getDisposition();
String contentType = p.getContentType();
String contents = null;
if(fileName != null)
{
fileName = saveFile(p,fileName);
}
else
{
if( p.isMimeType("text/plain") ||
p.isMimeType("text/html") ||
p.isMimeType("text/xml") )
{
Object o = p.getContent();
if(o instanceof String)
{
contents = (String) o;
}
else
{
fileName = saveFile(p,fileName);
}
}
else
{
fileName = saveFile(p,fileName);
}
}
if(fileName != null)
partc.insertAfter("filename", fileName);
if(disposition != null)
partc.insertAfter("disposition",disposition);
if(contentType != null)
partc.insertAfter("contentType",contentType);
if(contents != null)
partc.insertAfter("contents",contents);
}
catch (Exception e)
{
}
return part;
}
public static String saveFile(Part p, String seedFileName)
throws MessagingException, IOException
{
String fileName = “”;
if(seedFileName == null)
{
seedFileName = p.getContentType() + “.txt”;
}
seedFileName.replace('/','-');
InputStream is = p.getInputStream();
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
fileName = sdf.format(d) + "-" + seedFileName;
byte[] buffer = new byte[4096];
File directory = new File("/tmp");
File f = new File(directory,fileName);
int i = 0;
while(f.exists())
{
i++;
fileName = sdf.format(d) + i + "-" + seedFileName;
f = new File(directory,fileName);
}
FileOutputStream fos = new FileOutputStream(f);
int read = is.read(buffer);
while(read > -1)
{
fos.write(buffer,0,read);
read = is.read(buffer);
}
fos.flush();
fos.close();
return fileName;
}
Hi,
thank you for the java code. I have problems to compile this service:
please could you give the description for the output values
and Do you have a special CLASSPATH variable to do that ?
Thanks & Regards
Dom
Since it is Java service code the CLASSPATH is set for you.
If there still are compile errors. Please post the specific error.