Java Mail api: Read only latest replied message from mail body - java

I am using JavaMail api to read the mail, but I am facing problem while reading message from the replied mail.
This the the new reply
From: recipient Admin [mailto:test1#test.net]
Sent: 08 August 2016 19:04
To: abcd#test.COM
Subject: commented for test
I want to read only the replied message i.e"this is the new reply",
using
Multipart mp = (Multipart)p.getContent();
gives me the entire mail with the salutations of the sender as well, I want to just read the replied mail and not previous mail salutations.
I don't want differentiate on the basis on specific string as each mailing service would have different salutations.
Thank you!

If you do a bit of searching, you'll find that there's no simple solution to this problem. There is no standard for how the original message is embedded in the reply message and thus no standard way to extract it. The only solution is heuristics that handle the most common forms, but there's no guaranteed way to handle all possible cases.

Related

How to differentiate between the automatic reply and the normal mail received

I am working on receiving mails in my springboot application. In order to fetch and store the receive mails. I am using imap mail listener. There are two types of mails which I am storing. One is multipart payload type and the other is string payload type.
After receiving mail I am trying to send an auto-generated mails using java mail.
The issue which I am facing is worst case scenario of generating auto-reply from my application i.e infinite loop.
Can someone help ow can I differentiate between a normal mail received and auto-reply received in my mail box and generate auto-replies from my system only for those mails which are not auto-reply type.
It would be nice if explained via code for headers check. I came across through few headers like x-Autosubmitted. But they are returning null if I am trying to print the values in console.
The auto-submmitted markers are described below that you may find helpful:
auto-generated - Indicates that a message was generated by an automatic process, and is not a direct response to another message.
auto-replied - Indicates that a message was automatically generated as a direct response to another message.
auto-notified - Indicates that a message was generated by a Sieve notification system.
no - Indicates that a message was NOT automatically generated, but was created by a human. It is the equivalent to the absence of an Auto-Submitted header altogether.
The RFC 2822 states the following:
Though optional, every message SHOULD have a "Message-ID:" field.
Furthermore, reply messages SHOULD have "In-Reply-To:"
So, you may check for the "In-Reply-To:" value in the header.
Also you may add your own value to the outgoing email, so you may distinguish between an automatically generated reply from your system and manually created.

gmail is modifying header(Message-ID) of incoming mails

I sent a mail from my smtp server to gmail.
The message-id that I got is:
Message-ID: SMTPIN_ADDED_BROKEN#mx.google.com>
X-Google-Original-Message-ID:
Gmail appended extra parts in Message-id.
All I got on google that it can be an authentication issue.But source of mail shows:
SPF:PASS
But DKIM is not present in the source.
Do we require DKIM to be necessarily present in the mail's source??
What can I do to prevent header from being modified?
the msesage ID will be modified only if it does not obey RFC standard. so send mail with correct message id format
After many tries, I finally figured out that you need to add brackets enclosing the Message-Id
<yourid#domain.com>

java mail - getting message by message ID

I'm developing Android mail client. I need to build a "conversation" structure for every email message. I use the
IMAPMessage.getInReplyTo()
method that returns the Message ID of message which the message is a reply to. Unfortunatelly there seems to be no easy way to obtain message from
IMAPFolder
using its message ID. It is only possible to get the message by its UID. Is there an easy way to get the IMAP message by its Message ID?
You can use the IMAPFolder's search method like this:
SearchTerm searchTerm = new MessageIDTerm(messageId);
Message[] messages = imapFolder.search(searchTerm);
See the docs for the IMAPFolder's search method here:
https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/IMAPFolder.html#search-javax.mail.search.SearchTerm-
and for the MessageIDTerm class here:
https://javaee.github.io/javamail/docs/api/javax/mail/search/MessageIDTerm.html
unfortunately there is no straight forward solution... May be what you can try is to maintain an internal structure with body structures of all the mail ids and then perform a one on one Message-ID check and obtain the UID of mail. Anyways you would be doing it, in order to show the Maillist. Add a new logic to map the message-ids as well.

Java Mail API: Find if a mail is forwarded mail or a reply to another mail

I have a very special requirement
I am using Java Mail API to access a user's Inbox. You can say its like a service inbox for complaints. User registers a complaint by sending an email to this address. I fetch every email from Inbox and create a new complaint. My problem is that I don't want to create unnecessary complaints for reply or forwarded emails. Is there a way to find out that.
Most email clients add Re: or Fwd: to the subject line, but I wouldn't rely on that to determine whether an email is a reply or forwarded text; for example, a German mailclient might put Betr.: in front of the original subject.
Instead, you might want to look at the In-Reply-To: header and/or the References: header. See RFC 4021 for detailed information on those headers.
You can retrieve the headers from a Message by calling the getHeader(java.lang.String) method.
Reading the email header might help, see http://javamail.kenai.com/nonav/javadocs/javax/mail/Part.html#getAllHeaders(). In case of replies, the message header would include:
In-Reply-To: Message-ID of the message that this is a reply to
In case of a forwarded message, selected headers might be preserved. Not as straight-forward as the previous case, but it might still be possible to determine if it was a forwarded message. Refer to http://en.wikipedia.org/wiki/Email_forwarding#Manual_client-based_forwarding for details.

Create a javax.mail message of mimetype: application/EDIFACT

I Am working on an application that can send EDIFACT messages by mail.
The sending and receiving mail is no problem, receiving an EDIFACT message (send by another application) works as well.
EDIFACT is a standard to send business to business information (invoices for example)
The mail has this structure:
Message-ID: 20101110081058.CLOCKT..SRC.SRCNUMBER.edi01#SENDER.DOMAIN.com
From: EDI-berichten <edi01#SENDER.DOMAIN.com>
Date: Wed, 10 Nov 2010 08:10:57
To: <ekt#DOMAIN.nl>
Subject: INVOIC;SRC
MIME-VERSION: 1.0
Content-type: application/EDIFACT;
name=message.edi
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="message.edi"
X-OriginalArrivalTime: 10 Nov 2010 07:13:29.0843 (UTC) FILETIME=[C6CCD430:01CB80A6]
X-Virus-Scanned: clamav-milter 0.96.4 at pilot
X-Virus-Status: Clean
here you can find the EDIFACT specifications: http://www.faqs.org/rfcs/rfc1767.html
Normally you send an attachment by creating a MimeMultipart and add a text and an attach part to this.
The edifact message however, does not have a message part, just the attachment.
when I add the attachment part to the content, like this:
MimeBodyPart attachBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttach);
attachBodyPart.setDataHandler(new DataHandler(source));
attachBodyPart.setFileName(source.getName());
msg.setContent(attachBodyPart, "application/EDIFACT");
I get this error:
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/EDIFACT
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:930)
etc...
and finally this:
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/EDIFACT
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:877)
at javax.activation.DataHandler.writeTo(DataHandler.java:302)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888)
Does anyone have any experience in creating a message like this one, or how to create a message handler for a type like application/EDIFACT?
In receiving a EDIFACT message and saving the attachment I had also the problem it's not in the default way to save an attachment, using:
mp = (Multipart)message.getContent();
BodyPart part = mp.getBodyPart(i);
part.getInputStream()
I had to use this instead (adding this info, because it might be a lead to the solution):
SharedByteArrayInputStream sbaIs = (SharedByteArrayInputStream)message.getContent()
** EDIT 18 mar 2013 **
The minimal response to my question over here was a reason to figure it out myselve, and in the end I made it an open source porject. it already was usefull to some developers.
So have a look: use and please feedback on it: https://github.com/iamit/IAmEdifact
Yesterday very late I have finished working on the solution of the problem descibed above.
I could not find a proper answer anyweher on the internet.
So I created a page with steps of how to do this (documentation is not entirely finished, but it will be soon)
The site: https://sites.google.com/site/edifact4java/home
In short, you have to do this:
Add a DHC (DataContentHandler) to
the MailcapCommandMap
Create a
DataContentHandler for the mime-type
application/EDIFACT
Create a
MimePart customised for
application/EDIFACT
Create a
DataHandler for an edifact file or
message
Create a message
interface for edifact messages
And put all this together, and..... it works!!
A long puzzle, that's why I take the effort documenting it on a site. Hopefully somebody, some day can do his/her profit with it.

Categories

Resources