Reading Email Attachments Using JavaMail - Reading 'winmail.dat' Rather Than Actual Attachment - java

I am attempting to build a client that uses the javax.mail API to read email messages, including attachments. My initial thought was to use the Tika library. When that produced an unexpected result (please refer to code and details below), I tried using a variation of the code presented in this question.
My function for reading emails:
// Reference: https://www.tutorialspoint.com/javamail_api/javamail_api_checking_emails.htm
public class CheckingMails {
public static void check(String host, String storeType, String user, String password) {
try {
// create properties field
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", "110");
Session emailSession = Session.getInstance(properties);
Store store = emailSession.getStore("pop3");
store.connect(host, user, password);
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
Object content = message.getContent();
if (content instanceof java.lang.String) {
System.out.println((String) content);
} else if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for (int j = 0; j < mp.getCount(); j++) {
Part part = mp.getBodyPart(j);
String disposition = part.getDisposition();
if (disposition == null) {
// Check if plain
MimeBodyPart mbp = (MimeBodyPart) part;
if (mbp.isMimeType("text/plain")) {
System.out.println("Mime type is plain");
System.out.println((String) mbp.getContent());
} else {
// Special non-attachment cases here of
// image/gif, text/html, ...
System.out.println("File name is " + part.getFileName());
}
} else if ((disposition != null)
&& (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
// Check if plain
MimeBodyPart mbp = (MimeBodyPart) part;
if (mbp.isMimeType("text/plain")) {
System.out.println("Mime type is plain");
System.out.println((String) mbp.getContent());
} else {
System.out.println("Save file " + part.getFileName());
}
}
}
}
System.out.println("Tika results " + parseMsg(message));
}
// close the store and folder objects
emailFolder.close(false);
store.close();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
When I test the program by sending a test message with a simple CSV as an attachment, the Tika parser outputs the following (truncated for brevity):
------=_NextPart_000_0028_01D47C16.81FB45C0
Content-Type: application/ms-tnef;
name="winmail.dat"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="winmail.dat"
eJ8+IhUSAQaQCAAEAAAAAAABAAEAAQeQBgAIAAAA5AQAAAAAAADoAAEIgAcAGAAAAElQTS5NaWNy
b3NvZnQgTWFpbC5Ob3RlADEIAQOQBgCgDQAANgAAAAsAAgABAAAAAwAmAAAAAAALACkAAAAAAB4A
cAABAAAABQAAAFRlc3QAAAAAAgFxAAEAAAAWAAAAAdR8SMswwghurPF+SpeO8uRrTzrhCgAACwAB
DgAAAAACAQoOAQAAABg.....
------=_NextPart_000_0028_01D47C16.81FB45C0--
The other portion of the code (that checks for the message's disposition), outputs something similar to the following:
messages.length---1
Mime type is plain
Email Boday
Save file winmail.dat
Why is the application reading/recognizing winmail.dat and not the attachment that was actually sent with the message (a CSV file)?
Thanks for any help.

Related

javax.mail: Get nested attachments in EML attachment

I have an existing code that downloads and processes some emails correctly.
The email to process must have one or more xml as attachment, now I'm migrating this process from the current standard mail account to a certified system that wrap that mail into a new email.
So, instead of a flat email with one xml attachment, I have to parse an email with an XML (the certified) and an EML (the message that I should process).
In short, my code is like the following:
private void processMessage(final Message message) {
try {
final String contentType = message.getContentType();
if (contentType.contains("multipart")) {
final Multipart multiPart = (Multipart) message.getContent();
for (int i = 0; i < multiPart.getCount(); i++) {
final MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
/**************************************************************
* HERE I CAN'T GET THE EML (and its attachments) FROM 'part' *
**************************************************************/
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
processAttachment(part);
}
}
}
} [...cutted...]
}
private void processAttachment(final MimeBodyPart part) throws IOException, MessagingException {
final InputStream input = getReusableInputStream(part);
if (part.getFileName() != null && isXmlType(part.getContentType())) {
processXml(input);
}
}
I should modify it, in order to parse the EML and get the attachments recursively, but I'm missing the big picture.
UPDATE: I've modified the processAttachment method (but it still doesn't work):
private void processAttachment(final Multipart multipart) {
try {
for (int i = 0; i < multipart.getCount(); i++) {
final BodyPart bodyPart = multipart.getBodyPart(i);
if (bodyPart.getContent() instanceof Multipart) {
// part-within-a-part, do some recursion...
extractAttachment((Multipart) bodyPart.getContent());
}
System.out.println("Filename: " + bodyPart.getFileName());
System.out.println("ct: " + bodyPart.getContentType());
final boolean isXml = bodyPart.getFileName() != null && isXmlType(bodyPart.getContentType());
if (isXml) {
final InputStream inputStream = getReusableInputStream(bodyPart);
processXMLAttachment(inputStream);
}
}
} [cutted]
}
The output is:
Filename: null
ct: TEXT/PLAIN; charset=iso-8859-1
Filename: null
ct: TEXT/HTML; charset=iso-8859-1
Filename: daticert.xml
ct: APPLICATION/XML; name=daticert.xml
Filename: postacert.eml
ct: MESSAGE/RFC822; name=postacert.eml
Filename: smime.p7s
ct: APPLICATION/X-PKCS7-SIGNATURE; name=smime.p7s
From the output, I can see that the system only scaned the first level attachments daticert.xml and postacert.eml but it didn't find the nested attachments.
More specifically, I have to read the content of:
Filename: postacert.eml
ct: MESSAGE/RFC822; name=postacert.eml
Any help, please?
Thanks
Well, I solved by checking the class of any MimePart, and I found that nested messages are type of IMAPNestedMessage, so on this kind of object I recursively call the main method processMessage:
private void processAttachment(final Multipart multipart) {
try {
for (int i = 0; i < multipart.getCount(); i++) {
final BodyPart bodyPart = multipart.getBodyPart(i);
// BEGIN - Added this part
System.out.println("CLASS bodyPart: " + bodyPart.getContent().getClass());
if (bodyPart.getContent() instanceof IMAPNestedMessage) {
processMessage((IMAPNestedMessage) bodyPart.getContent());
} else {
// END - Added this part
if (bodyPart.getContent() instanceof Multipart) {
processAttachment((Multipart) bodyPart.getContent());
} else {
final boolean isXml = bodyPart.getFileName() != null && isXmlType(bodyPart.getContentType());
if (isXml) {
final InputStream inputStream = getReusableInputStream(bodyPart);
processXMLAttachment(inputStream);
}
}
}
}
} catch (final Exception e) {
sendMailService.sendMailForImportINPSFailed("metodo processAttachment()", e);
e.printStackTrace();
}
}
And now it works fine.

Call a java Rest webservice on email event

I have a webservice x exposed.If I get a mail in my email containing attachment I want to call
this webservice.I am not asking for code.I am asking your suggestion in high level how this can be done?
You need REST. So - you cant use org.apache.http.client library. (Other way, if you'll have web service you can use wsimport to create WebService java-client.)
Use javax.mail library to open mailbox and iterate letters.
Process letters. Mark or delete processed letters.
Add crontab/sheduler task to run your app. (As for me, It's better, than create a thread app). Dont forget to use run-one option to prevent double execution.
Here some code example that reads "*.xls" files from mailbox messages:
public void processEmail() throws MessagingException {
Store emailStore = null;
Folder emailFolder = null;
try {
// connecting
Properties properties = new Properties();
properties.setProperty("mail.store.protocol", "imaps");
Session emailSession = Session.getDefaultInstance(properties);
Main.getLog().debug("CONNECT TO : " + config.mailServer + " ["
+ config.mailUser + "] DEBUG: "+config.debug);
// check mailbox
emailStore = emailSession.getStore("imaps");
emailStore.connect(config.mailServer, config.mailUser,
config.mailPassword);
Main.getLog().debug("CONNECT DONE");
emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
Message[] messages = emailFolder.getMessages();
Main.getLog().debug("RECEIVED " + messages.length + " MESSAGSES");
// for each letter
for (Message message : messages) {
try {
Main.getLog().debug("\nPROCESS LETTER : "
+ message.getSubject());
if (message.getFlags().contains(Flags.Flag.DELETED)) {
continue; // don't process such letters
}
if (message.getFlags().contains(Flags.Flag.SEEN)) {
continue; // don't process such letters
}
//
Map<String, String> parseResult = new HashMap<String, String>();
String auditId = "";
// get file
if (message.getContent() instanceof Multipart) {
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart
.getDisposition())) {
continue;
}
// Process file
if (bodyPart.getFileName().contains(".xls")) {
auditId = maintainExcelFile(bodyPart.getInputStream());
}
}
} else if (message.getContent() instanceof BASE64DecoderStream) {
// Process file
if (message.getFileName().contains(".xls")) {
auditId = maintainExcelFile(((BASE64DecoderStream) message
.getContent()));
}
}
if (!config.debug && auditId!=null && !auditId.equals("")) {
message.setFlag(Flags.Flag.SEEN, true);
}
if (!config.debug) {
sendAcceptMail(message, auditId);
}
} catch (Exception e) {
// Process errors
if (!config.debug) {
message.setFlag(Flags.Flag.SEEN, true);
sendErrorMail(message, e);
}
Main.getLog().error(e.getMessage(), e);
throw new Exception(e);
}
}
} catch (Exception e) {
Main.getLog().error(e.getMessage(), e);
} finally {
emailFolder.close(true);
emailStore.close();
}
}

Sporadically getting javax.mail.FolderClosedException while reading gmail emails using javamail

I am trying to read javamail inbox and perform search. For this, I am fetching the latest 100 messages and then I am iterating through each to see if they have the sender for whom I am searching for. If its matching, I get its content via getContent().
Here is my javamail code snippet:
try {
Properties properties = new Properties();
properties.setProperty("mail.store.protocol", "imap");
properties.put("mail.imaps.starttls.enable", "true");
properties.put("mail.imap.socketFactory.port", "587");
System.setProperty("javax.net.debug", "ssl");
System.out.println("prop " + properties.getProperty("mail.smtp.port"));
Session session = Session.getDefaultInstance(properties, null);
// session.setDebug(true);
Store store = null;
store = session.getStore("imaps");
store.connect("imap.gmail.com", username, password);
Folder inbox;
inbox = store.getFolder("Inbox");
/* Others GMail folders :
* [Gmail]/All Mail This folder contains all of your Gmail messages.
* [Gmail]/Drafts Your drafts.
* [Gmail]/Sent Mail Messages you sent to other people.
* [Gmail]/Spam Messages marked as spam.
* [Gmail]/Starred Starred messages.
* [Gmail]/Trash Messages deleted from Gmail.
*/
inbox.open(Folder.READ_WRITE);
Message msgs[] = inbox.getMessages(inbox.getMessageCount() - lastHistory, inbox.getMessageCount());
System.out.println("MSgs.length " + msgs.length);
ArrayList<Message> aList = new ArrayList<Message>();
appendTextToConsole("Searching for appropriate messages!!");
for (int ii = msgs.length - 1; ii >= 0; ii--) {
Message msg = msgs[ii];
Address[] in = msg.getFrom();
String sender = InternetAddress.toString(in);
System.out.println((++index) + "Sender: " + sender);
boolean read = msg.isSet(Flags.Flag.SEEN);
if (sender.contains(googleId) && !read) {
//This line below gives FolderClosedException sporadically
Object content = msg.getContent();
if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
if (Pattern
.compile(Pattern.quote("text/html"),
Pattern.CASE_INSENSITIVE)
.matcher(bp.getContentType()).find()) {
// found html part
String html = (String) bp.getContent();
Element element = Jsoup.parse(html);
List<Element> anchors = element.getElementsByTag("a");
for (Element e : anchors) {
if (e.attr("href").startsWith("https://www.google.com/url?rct=j&sa=t&url=")
&& !e.attr("style").equalsIgnoreCase("text-decoration:none")) {
String url = e.attr("href");
String title = e.text();
String agency = e.parent().parent().child(1).child(0).child(0).text();
String message = e.parent().parent().child(1).child(0).child(1).text();
String flagUrl = e.parent().parent().child(1).child(1).child(0).child(0).child(3).child(0).attr("href");
System.out.println("URL: " + url);
System.out.println("Title: " + title);
System.out.println("agency: " + agency);
System.out.println("Message: " + message);
System.out.println("flagURL: " + flagUrl);
AbstractMessage ams = new AbstractMessage(url, title, agency, message, flagUrl);
aMsgs.add(ams);
}
}
//System.out.println((String) bp.getContent());
} else {
// some other bodypart...
}
}
try {
inbox.close(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
appendTextToConsole("Done searching for appropriate messages!!");
} catch (Exception mex) {
appendTextToConsole(mex.getMessage());
mex.printStackTrace();
}
But the most irritating thing is that after fetching a few messages, sporadically a javax.mail.FolderClosedException is thrown due to unknown reasons. Now my question is that how do I deal with this scenario? And how do ideal mail clients made using javamail deal with it?
Turn on session debugging and you might get more clues as to what's going on.
Note that the server will close the connection if you're not using it.
And of course all sorts of network failures are possible.

Retrieving all unread emails using javamail with POP3 protocol

I am trying to access my gmail account and retrieve the information of all unread emails from that.
I have written my code after referring many links. I am giving a few links for reference.
Send & Receive emails through a GMail account using Java
Java Code to Receive Mail using JavaMailAPI
To test my code, I created one Gmail account. So I received 4 messages in that from Gmail.
I run my application after checking number of mails. That showed correct result. 4 unread mails.
All the infomation was being displayed (e.g. date, sender, content, subject, etc.)
Then I logged in to my new account, read one of the emails and rerun my application.
Now the count of unread message should have been 3, but it displays "No. of Unread Messages : 0"
I am copying the code here.
public class MailReader
{
Folder inbox;
// Constructor of the calss.
public MailReader() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(
System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995, "abc#gmail.com",
"paasword");
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox */
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + messages.length);
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
printAllMessages(messages);
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (MessagingException e)
{
System.out.println("Exception while connecting to server: "
+ e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception
{
for (int i = 0; i < msgs.length; i++)
{
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception
{
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date : " + receivedDate.toString());
}
System.out.println("Sent Date : " + sentDate.toString());
System.out.println("Content : " + content);
getContent(message);
}
public void getContent(Message msg)
{
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 void dumpPart(Part p) throws Exception {
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1) {
System.out.write(c);
}
}
public static void main(String args[]) {
new MailReader();
}
}
I searched on google, but I found that you should use Flags.Flag.SEEN to read unread emails.
But thats not showing correct results in my case.
Can someone point out where I might be doing some mistake?
If you need whole code, I can edit my post.
Note: I edited my question to include whole code instead of snippet I had posted earlier.
Your code should work. You can also use the Folder.getUnreadMessageCount() method if all you want is the count.
JavaMail can only tell you what Gmail tells it. Perhaps Gmail thinks that all those messages have been read? Perhaps the Gmail web interface is marking those messages read? Perhaps you have another application monitoring the folder for new messages?
Try reading an unread message with JavaMail and see if the count changes.
You might find it useful to turn on session debugging so you can see the actual IMAP responses that Gmail is returning; see the JavaMail FAQ.
You cannot fetch unread messages with POP3. From JavaMail API:
POP3 supports no permanent flags (see Folder.getPermanentFlags()). In
particular, the Flags.Flag.RECENT flag will never be set for POP3
messages. It's up to the application to determine which messages in a
POP3 mailbox are "new".
You can use IMAP protocol and use the SEEN flag like this:
public Message[] fetchMessages(String host, String user, String password, boolean read) {
Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore();
store.connect(host, user, password);
Folder emailFolder = store.getFolder("INBOX");
// use READ_ONLY if you don't wish the messages
// to be marked as read after retrieving its content
emailFolder.open(Folder.READ_WRITE);
// search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, read);
return emailFolder.search(unseenFlagTerm);
}
Another thing to notice is that POP3 doesn't handle folders. IMAP gets folders, POP3 only gets the Inbox. More info at: How to retrieve gmail sub-folders/labels using POP3?
Change inbox.open(Folder.READ_ONLY); to inbox.open(Folder.READ_WRITE);
It will change your mail as read in inbox.
Flags seen = new Flags(Flags.Flag.RECENT);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
messages = inbox.search(unseenFlagTerm);
The correct flag to use is
Flags.Flag.RECENT
please use this method to get the unread mails
getNewMessageCount()
refer below link:
https://javamail.java.net/nonav/docs/api/com/sun/mail/imap/IMAPFolder.html

Can't get email attachments without extension from javax.mail.message

Help please to solve my problem.
I have a function that find email content and attachments:
public void setContentAndAttachmentsList(GMailEmail gMailEmail, Part message) throws MessagingException, IOException {
if (message.isMimeType("text/*") && StringUtils.isEmpty(message.getFileName())) {
gMailEmail.setContent(message.getContent().toString());
log.debug("Found text data. Set as body: " + message.getContent().toString());
} else if (message.isMimeType(MULTIPART_MIME_TYPE)) {
Multipart multipartMessage = (Multipart) message.getContent();
for (int i = 0; i < multipartMessage.getCount(); i++) {
BodyPart messagePart = multipartMessage.getBodyPart(i);
setContentAndAttachmentsList(gMailEmail, messagePart);
}
} else if (StringUtils.isNotBlank(message.getFileName())) {
MailAttachment attachment = new MailAttachment();
attachment.setContentType(message.getContentType());
log.debug("Attachment content type: " + message.getContentType());
attachment.setName(message.getFileName());
log.debug("Attachment file name: " + message.getFileName());
if (message.getContent() instanceof InputStream) {
attachment.setInputStream((InputStream) message.getContent());
} else {
attachment.setInputStream(IOUtils.toInputStream(message.getContent().toString(), UTF_8));
}
List<MailAttachment> attachmentsList = gMailEmail.getAttachmentsList();
attachmentsList.add(attachment);
gMailEmail.setAttachmentsList(attachmentsList);
log.debug("Found attachment" + message.getFileName());
}
}
GMailEmail is POJO.
If email has attachments that have extensions it works perfectly. But when attachments have no extensions, message.getFileName() simply returns null.
I solved this problem.
It must be
StringUtils.isBlank(message.getFileName())
instead of
StringUtils.isEmpty(message.getFileName())
in second line.

Categories

Resources