EWS Java API get Attachment - java

I have some issue in getting some attachment using ews java API 1.3 SNAPSHOT, i want to get Attachment in my email, here my code :
try {
ExchangeService service;
service.setUrl(new URI("https://" + myserver + "/ews/Exchange.asmx"));
ExchangeCredentials credentials = new WebCredentials(username, password);
service.setCredentials(credentials);
ItemView view = new ItemView(Integer.MAX_VALUE);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
Folder folder = Folder.bind(service, WellKnownFolderName.Inbox);
FindItemsResults<Item> results = service.findItems(folder.getId(),view);
service.loadPropertiesForItems(results, new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));
for (Item item : results) {
Item itm = Item.bind(service, item.getId(), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));
EmailMessage emailMessage = EmailMessage.bind(service, itm.getId(), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));
if (emailMessage.getHasAttachments()) {
for (Attachment attachment : emailMessage.getAttachments()) {
String FileExtension = getFileExtension(attachment.getName());
File TempFile = File.createTempFile(attachment.getName(), FileExtension);
attachment.load(TempFile.getAbsolutePath());
}
}
}
} catch (Exception e) {
logger.error("Error ", e.getMessage());
}
My issue is it can get another email that has no attachment and always skip email that has an attachment, the example is like this,
In my inbox i have this email list
from: a#gmail.com (has attachment)
from: b#mycompany.com (no attachment)
from: c#hiscompany.com (has attachment)
from: d#mycompany.com (no attachment)
And when i run my code, it always get email that has no attachment, like this:
from: b#mycompany.com (no attachment)
from: d#mycompany.com (no attachment)
and skip the other email that has attachment, i have no idea how can this happen. Can someone help me please?

HashMap<String, HashMap<String, String>> attachments = new HashMap<String, HashMap<String, String>>();
if (emailMessage.getHasAttachments() || emailMessage.getAttachments().getItems().size() > 0) {
//get all the attachments
AttachmentCollection attachmentsCol = emailMessage.getAttachments();
log.info("File Count: " +attachmentsCol.getCount());
//loop over the attachments
for (int i = 0; i < attachmentsCol.getCount(); i++) {
Attachment attachment = attachmentsCol.getPropertyAtIndex(i);
//log.debug("Starting to process attachment "+ attachment.getName());
//FileAttachment - Represents a file that is attached to an email item
if (attachment instanceof FileAttachment || attachment.getIsInline()) {
attachments.putAll(extractFileAttachments(attachment, properties));
} else if (attachment instanceof ItemAttachment) { //ItemAttachment - Represents an Exchange item that is attached to another Exchange item.
attachments.putAll(extractItemAttachments(service, attachment, properties, appendedBody));
}
}
}
} else {
log.debug("Email message does not have any attachments.");
}
//Extract File Attachments
try {
FileAttachment fileAttachment = (FileAttachment) attachment;
// if we don't call this, the Content property may be null.
fileAttachment.load();
//extract the attachment content, it's not base64 encoded.
attachmentContent = fileAttachment.getContent();
if (attachmentContent != null && attachmentContent.length > 0) {
//check the size
int attachmentSize = attachmentContent.length;
//check if the attachment is valid
ValidateEmail.validateAttachment(fileAttachment, properties,
emailIdentifier, attachmentSize);
fileAttachments.put(UtilConstants.ATTACHMENT_SIZE, String.valueOf(attachmentSize));
//get attachment name
String fileName = fileAttachment.getName();
fileAttachments.put(UtilConstants.ATTACHMENT_NAME, fileName);
String mimeType = fileAttachment.getContentType();
fileAttachments.put(UtilConstants.ATTACHMENT_MIME_TYPE, mimeType);
log.info("File Name: " + fileName + " File Size: " + attachmentSize);
if (attachmentContent != null && attachmentContent.length > 0) {
//convert the content to base64 encoded string and add to the collection.
String base64Encoded = UtilFunctions.encodeToBase64(attachmentContent);
fileAttachments.put(UtilConstants.ATTACHMENT_CONTENT, base64Encoded);
}
//Extract Item Attachment
try {
ItemAttachment itemAttachment = (ItemAttachment) attachment;
PropertySet propertySet = new PropertySet(
BasePropertySet.FirstClassProperties, ItemSchema.Attachments,
ItemSchema.Body, ItemSchema.Id, ItemSchema.DateTimeReceived,
EmailMessageSchema.DateTimeReceived, EmailMessageSchema.Body);
itemAttachment.load();
propertySet.setRequestedBodyType(BodyType.Text);
Item item = itemAttachment.getItem();
eBody = appendItemBody(item, appendedBody.get(UtilConstants.BODY_CONTENT));
appendedBody.put(UtilConstants.BODY_CONTENT, eBody);
/*
* We need to check if Item attachment has further more
* attachments like .msg attachment, which is an outlook email
* as attachment. Yes, we can attach an email chain as
* attachment and that email chain can have multiple
* attachments.
*/
AttachmentCollection childAttachments = item.getAttachments();
//check if not empty collection. move on
if (childAttachments != null && !childAttachments.getItems().isEmpty() && childAttachments.getCount() > 0) {
for (Attachment childAttachment : childAttachments) {
if (childAttachment instanceof FileAttachment) {
itemAttachments.putAll(extractFileAttachments(childAttachment, properties, emailIdentifier));
} else if (childAttachment instanceof ItemAttachment) {
itemAttachments = extractItemAttachments(service, childAttachment, properties, appendedBody, emailIdentifier);
}
}
}
} catch (Exception e) {
throw new Exception("Exception while extracting Item Attachments: " + e.getMessage());
}

Related

multipart/alternative getContent() throws IOException: No content

i have a problem with a code of mine. I try to get the text of an email from my Mailserver. After I retrieve the mail, I analyse the message and try to get the bodypart with the type "text/plain" and/or "text/html".
I use following method to split up a MimeMultipart:
private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException{
String result = "";
int count = mimeMultipart.getCount();
BodyPart bodypart;
String html;
System.out.println("Number of parts: "+count);
for (int i = 0; i < count; i++) {
bodypart=mimeMultipart.getBodyPart(i);
System.out.println("Type of part "+(i+1)+" "+bodypart.getContentType());
System.out.println("Class: "+bodypart.getClass());
if(bodypart.isMimeType("text/plain")){
try{
//if(bodypart==null){
// System.out.println("Null in bodypart");
//}else{
System.out.println((String)bodypart.getContent());//cant get Content!
//}
}catch (IOException e){
System.out.println("IO Exception on attachment"+(i+1)+": "+e+"\n");
}
//result=(String) bodypart.getContent();
}else if (bodypart.isMimeType("text/html")){
try{
html = (String) bodypart.getContent();//cant get Content!
System.out.println(org.jsoup.Jsoup.parse(html).text());
//result = org.jsoup.Jsoup.parse(html).text();
}catch (IOException e){
System.out.println("IOException on part"+(i+1)+": "+e+"\n");
}
}else if (bodypart.isMimeType("multipart/*")){
System.out.println("Openning Multipart bodypart...!\n");
getTextFromMimeMultipart((MimeMultipart) bodypart.getContent());
}else{
System.out.println("No none part?!: "+bodypart.getContentType());
}
}
return result;
}
My Problems lies in .getContent(). it always throws IOException: no Content, eventhough i know there is a text in my mail.
Following output was generated:
Trying:***mail Subject***...
-------------------------------------------------------------------------------
Number of parts: 1
Type of part 1 multipart/alternative;
boundary=_000_************************************************_(snippedout)
Class: class com.sun.mail.imap.IMAPBodyPart
Openning Multipart bodypart...!
Number of parts: 2
Type of part 1 text/plain; charset=Windows-1252
Class: class com.sun.mail.imap.IMAPBodyPart
IO Exception on attachment1: java.io.IOException: No content
Type of part 2 text/html; charset=Windows-1252
Class: class com.sun.mail.imap.IMAPBodyPart
IOException on part2: java.io.IOException: No content
Do I miss something crucial? Am i handling the bodypart correct?
Additional note:
Sorry for not providing the calling function. I have indeed opened the folder
Funktion it is called from:
public void findJunkonBody (String pfolder, String pdestfolder)throws MessagingException, IOException{
Folder folder ;
Store store = null;
Folder destfolder;
MailUtil mutil = new MailUtil(smtphost,user,pass,"imap");
String [] junktermsbody=new String[1];
junktermsbody[0]="Empfangsbestätigung";
try{
store=buildIMAPStore();
store.connect(smtphost,user,pass);
folder = store.getFolder(pfolder);
destfolder = store.getFolder(pdestfolder);
folder.open(Folder.READ_WRITE);
Message[] msgs = folder.getMessages();
int i;
int j;
MimeMultipart mmpart;
for (i=0; i<=(msgs.length-1);i++){
for(j=0;j<=(junktermsbody.length-1);j++){
if (msgs[i].isMimeType("text/plain")){
if(msgs[i].getContent().toString().contains(junktermsbody[j])){
System.out.println("Palin text: "+msgs[i].getSubject());
//move logik
}
}else if (msgs[i].isMimeType("multipart/*")){
mmpart =(MimeMultipart) msgs[i].getContent();
System.out.println("Trying:"+msgs[i].getSubject()+"...\n-------------------------------------------------------------------------------");
if (getTextFromMimeMultipart(mmpart).contains(junktermsbody[j])){
System.out.println("Time to move it!");
//move logik
}
}
}
}
folder.close(true);
}finally{
if (store != null) {store.close();}
//System.out.println("Junk on Body moved successfully");
System.out.println();
}
}
When it comes to plain/text messages you need to open first the java server mail folder.
You need to open the folder before reading the content.
Folder folder = message.getFolder();
// Open folder in read-only mode
if (folder.isOpen()) {
if ((folder.getMode() & Folder.READ_WRITE) != 0) {
folder.close(false);
folder.open(Folder.READ_ONLY);
}
} else {
folder.open(Folder.READ_ONLY);
}
now you can read the message body content:
Object content = message.getContent();

java mail polling read inline or embedded images (smileys) from the mail

I am new in java mail polling and i have create one type of conversation application in this if user send mail to each other then i read that from them mail and post as new message in conversation.
now issue is that, what to do if there is smileys, inline or embedded images. for example in gmail mail we can send smileys also now how read that smile and post on to the over page. please give me some proper solution for this.
I have found the solution to download the inline images + icons from the mail.
private String getAttachments(Message message, HttpServletRequest request) throws MessagingException, IOException {
String contentType = message.getContentType();
String attachFiles="";
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
String disposition =part.getDisposition();
String file=part.getFileName();
//External attachments
if (disposition != null && Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
// this part is attachment
String fileName = new Date().getTime()+ "_"+ part.getFileName().replaceAll("[^a-zA-Z0-9\\._]+", "_"); //To make attachment name uniq we are adding current datatime before name.
attachFiles += fileName + ","; //concrete all attachment's name with comma separated.
part.saveFile(new File(request
.getSession()
.getServletContext()
.getRealPath(
"/WEB-INF/attechments/"
+ fileName))); //To save the attachment file at specific location.
// LOG.info("\n\t Path :- " +request.getSession().getServletContext().getRealPath("/WEB-INF/attechments/" + fileName));
}
//Inline Attachments
else if (disposition != null && Part.INLINE.equalsIgnoreCase(disposition)) {
// this part is attachment
String fileName = new Date().getTime()+ "_"+ part.getFileName().replaceAll("[^a-zA-Z0-9\\._]+", "_"); //To make attachment name uniq we are adding current datatime before name.
// attachFiles += fileName + ","; //concrete all attachment's name with comma separated.
part.saveFile(new File(request
.getSession()
.getServletContext()
.getRealPath(
"/WEB-INF/attechments/"
+ fileName))); //To save the attachment file at specific location.
// LOG.info("\n\t Path :- " +request.getSession().getServletContext().getRealPath("/WEB-INF/attechments/" + fileName));
}
//Inline icons and smileys
else if(file != null && disposition==null)
{
String fileName = new Date().getTime()+ "_"+ part.getFileName().replaceAll("[^a-zA-Z0-9\\._]+", "_");
// attachFiles += fileName + ","; //concrete all attachment's name with comma separated.
part.saveFile(new File(request
.getSession()
.getServletContext()
.getRealPath(
"/WEB-INF/attechments/"
+ fileName)));
}
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 1);
}
return attachFiles;
}

Best way to save email, including images and HTML data, using Java Mail API?

I'm looking for the best way to save an email body which includes inline images and HTML content. I want to Retain everything the mail contains.
My ultimate Goal is to save the complete email body into a PDF
If there is a direct way to write email body into PDF ?
if not what would be the best format to save the email ?
I can convert HTML, DOC etc to PDF using some other available API.
private void downloadAttachment(Part part, String folderPath) throws Exception {
String disPosition = part.getDisposition();
String fileName = part.getFileName();
String decodedText = null;
logger.info("Disposition type :: " + disPosition);
logger.info("Attached File Name :: " + fileName);
if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) {
logger.info("DisPosition is ATTACHMENT type.");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName != null && disPosition == null) {
logger.info("DisPosition is Null type but file name is valid. Possibly inline attchment");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName == null && disPosition == null) {
logger.info("DisPosition is Null type but file name is null. It is email body.");
File file = new File(folderPath + File.separator + "mail.html");
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
}
}
protected int saveEmailAttachment(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = null;
InputStream is = null;
int ret = 0, count = 0;
try {
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
part.writeTo(new FileOutputStream(saveFile));
} finally {
try {
if (bos != null) {
bos.close();
}
if (is != null) {
is.close();
}
} catch (IOException ioe) {
logger.error("Error while closing the stream.", ioe);
}
}
return count;
}
Please suggest. Thank you!
Save it in its natural state, as a MimeMessage.
JavaMail MimeMessages can be streamed to text, since that's how they arrive in mail. For example, MimeMessage.writeTo saves the message out as text. Similarly, MimeMessage.parse reads it back in. One in a MimeMessage, you can get the text, the attachments, etc. quite easily.
You could also stream it out as a serialized Java object, but, frankly, I wouldn't. The text representation is much more useful.

downloading email body containing inline images in java

My issue goes as follows:
I have My code setup to read emails from a particular account. That part works perfectly.
the issue is with parsing the Email message. Separating attachments and email body(containing inline images).
My code goes like this:
Void readMessages(Folder folder){
Message[] messages = folder.getMessages();
// loading of message objects.
for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) {
final Message currentMessage = messages[messageNumber];
logger.info("Handling the mail with subject " + currentMessage.getSubject());
logger.info("Content type for the current message is " + currentMessage.getContentType());
final String messageFileName = currentMessage.getFileName();
logger.info("File name for the message " + messageFileName + ". File name is blank "
+ StringUtils.isBlank(messageFileName));
Object messageContentObject = currentMessage.getContent();
if (messageContentObject instanceof Multipart) {
Multipart multipart = (Multipart) messageContentObject;
// downloading all attachments....
int attachmentCount = multipart.getCount();
logger.info("Number of attachments ");
for (int i = 0; i < attachmentCount; i++) {
Part part = (Part) multipart.getBodyPart(i);
downloadAttachment(part, folderPath.toString());
}
}
}
}
}
private void downloadAttachment(Part part, String folderPath) throws Exception {
String disPosition = part.getDisposition();
String fileName = part.getFileName();
String decodedText = null;
logger.info("Disposition type :: " + disPosition);
logger.info("Attached File Name :: " + fileName);
if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) {
logger.info("DisPosition is ATTACHMENT type.");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName != null && disPosition == null) {
logger.info("DisPosition is Null type but file name is valid. Possibly inline attchment");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName == null && disPosition == null) {
logger.info("DisPosition is Null type but file name is null. It is email body.");
File file = new File(folderPath + File.separator + "mail.html");
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
}
}
protected int saveEmailAttachment(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = null;
InputStream is = null;
int ret = 0, count = 0;
try {
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
part.writeTo(new FileOutputStream(saveFile));
} finally {
try {
if (bos != null) {
bos.close();
}
if (is != null) {
is.close();
}
} catch (IOException ioe) {
logger.error("Error while closing the stream.", ioe);
}
}
return count;
}
The problem i get is when i run this code, i get an HTML file but the inline images is replaced by a sign for error image which indicates the image with no source.
Please help me out with. Let me know if any more info is required.
I also tried saving the body as an .eml file by changing:
File file = new File(folderPath + File.separator + "mail.html");
to
File file = new File(folderPath + File.separator + "mail.eml");
BUt i got the same results.
I wrote below code to convert email body text to pdf including inline images.
in code i replaced the image code(ex: cid:image001.jpg#01D17AAA.1EA2A6A0) with download image path. I am constructing the "hashmap" for image key and download path while downloading the image.
HTMLWorker htmlWorker = new HTMLWorker(document);
if(bodyStr!=null)
{
//find inline images
inlineImages=downloadInLineImage(mostRecentMatch, dynamicOutputDirectory);
if(inlineImages!=null)
{
for (Map.Entry<String, String> entry : inlineImages.entrySet()) {
//System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
bodyStr=bodyStr.replaceAll("cid:"+entry.getKey() , entry.getValue());
}
}
htmlWorker.parse(new StringReader(bodyStr));
}
Download Inline image with passing Item.
private HashMap<String,String> downloadInLineImage(Item item, String dynamicOutputDirectory)
throws Exception, ServiceLocalException {
//create output directory if not present
//bind the item to a new email message. if you do not bind, then the getHasAttachments() function will fail
EmailMessage mostRecentMatch = (EmailMessage)item;
String from = mostRecentMatch.getFrom().getAddress();
String user =StringUtils.substringBefore(from, "#");
AttachmentCollection collection=item.getAttachments();
HashMap<String,String> inlineFiles=new HashMap<String,String>();
if(collection.getCount()>0)
{
for (Attachment attachment : collection.getItems()) {
if(attachment.getIsInline())
{
FileAttachment currentFile = (FileAttachment) attachment;
String filePath=dynamicOutputDirectory+"/"+user+currentFile.getName();
File file=new File(filePath);
FileOutputStream fio=new FileOutputStream(file);
currentFile.load(fio);
inlineFiles.put(currentFile.getContentId(), filePath);
fio.close();
}
}
}
References to inlined images are replaced by cid: URNs like <img src="cid:SOMEID">, because there are no filenames in an email. SOMEID refers to the Content-ID of the Multipart "objects".
In order to get it work, you have to store the multipart attachments to files (e.g., temporary names) and replace the cid URNs by the real file names.

Best view to display images and html content in android

*I am working on an application which can read emails. I am using textview / edittext to display the mails. Right now I am able to fetch the email content as string and display it.
But Which is the best view to display emails with html content and images??
Please help me out.
Thank you :)
Edited:
I used webview as suggested by S.O. friends(thanks to #Andro Selva, #CFlex).
But I am facing a problem, Its displaying the email's body twice! :(
Once as a text/plain and again as a Text/html.
I have tried following codes to load webview.
webViewBody.loadData(details[3], "text/html", "UTF-8");
//webViewBody.loadDataWithBaseURL(null, details[3] , "text/html", "utf-8", null);
here details[3] is the email content.
What am i missing here!? :( Please help me out.
EDITED:
I have Added codes I have Used to get the message contents for displaying email content below.
public void getContent(Message msg)
{
try
{
Object o = msg.getContent();
if (o instanceof String)
{
if(((String) o).equalsIgnoreCase(""))
{
}
else
{
messageBody = (String)o+"STRING!!";
}
}
else if (o instanceof Multipart)
{
Multipart mp = (Multipart)o;
int count3 = mp.getCount();
for (int j = 0; j < count3-1; j++)
{
// Part are numbered starting at 0
BodyPart b = mp.getBodyPart(j);
Object o2 = b.getContent();
if (o2 instanceof String)
{
if(((String) o).equalsIgnoreCase(""))
{
}
else
{
messageBody = (String)o2+"MULTIPART!!";
}
}
} //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)
//messageBody = convertStreamToString(is)+"INPUT STREAM!!";
int c;
while ((c = is.read()) != -1)
{
messageBody = convertToString(is)+"INPUT STREAM!!";
System.out.println(messageBody);
}
}
}
catch (Exception ex)
{
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
//TODO TEST CODES
try
{
String contentType = msg.getContentType();
// System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++)
{
dumpPart(mp.getBodyPart(i));
}
}
catch (Exception ex)
{
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
}
public String convertToString(InputStream inputStream)
{
StringBuffer string = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
try
{
while ((line = reader.readLine()) != null)
{
string.append(line + "\n");
}
}
catch (IOException e)
{
}
return string.toString();
}
The control is always going to "else if (o instanceof InputStream)" condition. so its streaming email always and displaying complete email content. What am I missing here?
I think you will need to implement a WebView, because it can format the html AND display images.
AFAIK, A textView with html can format the content but cannot display the images.
Edit:
The following should work:
webViewBody.loadData(details[3], "text/html", null);
Probably you are displaying all the email content that can be composed by html and plain text content in the same email.
This is caused because this kind of email is multipart content and u need to split-it, you also need to separate attachments and images (embedded)
see if your "details" is not returning a multipart content body.
The detail gets displayed twice just because of two times you have written:
webViewBody.loadData(details[3], "text/html", "UTF-8");
webViewBody.loadDataWithBaseURL(null, details[3] , "text/html", "utf-8", null);
Probable Solution: Use either loadData() or loadDataWithBaseURL().
adding these lines solved the problem!
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Thanks to everyone who helped me to solve this. :)

Categories