I'm using JavaMail 1.5.2 to read messages from IMAP accounts. To reduce the number of requests to the host I prefetch some message data, like From, Date, Message-ID etc.:
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
fp.add("Message-ID");
Message msgs[] = folder.getMessages();
folder.fetch(msgs,fp);
However, I want to also prefetch some parts of the content to create a preview text for the mail without having to load the full message with all attachments. For example, I would like to prefetch all parts of the content that have the type "text/plain" and are no attachments. Is that possible?
PS: I'm not searching for a solution like fp.add(IMAPFolder.FetchProfileItem.MESSAGE) because this will prefetch the whole message with all attachments.
You have to retrieve the bodystructure first, then loop across the message structure, check the mime type of each part, and download the parts you want. IMAP lets you download all of the parts using one command, so if Javamail is a little smart, you should be able to do this with two IMAP commands, no matter how many bodyparts you end up wanting to download.
The IMAP commands, if you're the type who likes to look at wire traffic, should be something like a uid fetch 234789 bodystructure followed by b uid fetch 234789 (body.peek[1.1] body.peek[2]).
Related
Let's say there is an attachment in the slack channel . And that attachment has some unique identifier So if I type the identifier i should get the attachment as response in the channel.
Example: if i type 45 and its related to an xml file attached in the slack channel, then it should reply with the uploaded attachment in the conversation.
I tried using GET method for https://slack.com/api/channels.history?&channel=<>&count=1&pretty=1&inclusive=true&token=<> and I obtained the history of the conversation in the channel.
Don't know how timestamp and unfurl can help in achieving this.
The API method channels.history returns a list of messages from a specific channel as a big JSON array. It will only return the 100 latest by default and you have to use paging if your channel contains more messages.
Messages are referenced by timestamp (ts). Attachments are elements of its message and references by ID (id), which represents the order their are shown on Slack.
So to access a specific attachment you first need to find the correct message by its timestamp and then you can find the attachment by its ID.
If you know the timestamp of the message you are interested in you can include latest=timestamp and oldest=timestamp in your API call to receive only that message.
If you do not know the timestamp of the message you will have to retrieve all messages within a reasonable timeframe and then detect your message based on some other criteria.
Btw. I would consider switching to conversations.history, which is the new and recommend API method for retrieving messages from all type of channels.
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.
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.
i need to implement the email signature with image.As of now we only support the text in email signature which is already working.i need to provide the functionality
where i can insert the image inside mail signature. i can send the email to user within myapplication and also to user on external mail domain like gmail,yahoo etc. When
mail is sent to some user with in my application system, system makes entryt o DB and when receiver receives in inbox (which internally read the mail from db). Now if user
send the mail to external user on gmail it makes use of javax mail api . Similary i can receive the email from external mail domains(gmail,yahoo etc) Now i have
few questions based on tis requirement:-
1)Is there any standard for how the external mail domains like gmail send the image inside signature to another domains like (my application mail domain)?
Another point related to it gmail user can have two images ,one for signature and another image inside body. How will i determine which image belongs to
signature? Is there any defined property for that?
2)Also not able to make out what is the best/consistent approach to send(whether to internal application user or external mail domain user ) the email signature containing
image so that it renders correctly when user receives it?
what I had in my mind for point 2:- i earlier thought i can use solution suggested at How to display an image in jsp?. where
with tag <.img src="/getImage.action?imageId=123">, i can fetch the image from db in action class or servlet and return. But keeping in mind
once i send the mail to the user on gmail , he will not be able to access the servlet.So this approach does not seems to fit in requirement.
Then i came across the another great stackoverflow link base64 encoded images in email signatures where
solution by Tim Medora looked great but again the comment below the solution Gmail doesn't seem to support it again ended my Folks
really i think i should be done if mail domain like gmail,yahoo support the solution suggested by because in that case i can send image as base64 string instead
of image as attachment.
Folks would be really grateful if you can provide me some pointer/approach regarding both points 1 and 2
To include images in the email message, first you have to include the images as MIME attachments in the email. Each of these attachments must have a "Content-ID" header.
--f46d0444ea0d6991ba04b91c92e6
Content-Type: image/gif; name="theImage.gif"
Content-Transfer-Encoding: base64
Content-ID: <theImage#abcd>
[base64 string]
--f46d0444ea0d6991ba04b91c92e6--
2) Then, in the email message, include the Content-ID in the src attribute of the <img> tag.
<img src="cid:theImage#abcd" />
For Gmail to see the embedded image from byte array, I posted an answer on another similar question which is to use ByteArrayDataSource and embed it to the HtmlEmail. Here's the code snippet:
import javax.mail.util.ByteArrayDataSource;
import org.apache.commons.mail.ImageHtmlEmail;
...
ImageHtmlEmail email = new ImageHtmlEmail();
byte[] qrImageBytes = createQRCode(); // get your image byte array
ByteArrayDataSource qrImageDataSource = new ByteArrayDataSource(qrImageBytes, "image/png");
String contentId = email.embed(qrImageDataSource, "QR Image");
Is there a way to fetch the mail bodies of multiple emails with a single call to an IMAP server using the Javamail API?
I know I can get to the body of a given message using the Message.getContent() call, but this ends up doing a call to the imap server for each individual message.
Is it possible to use the FetchProfile and Folder.fetch call to bulk fetch bodies? The documentation implies that the FetchProfile is only for header data. I tried the following, but that didn't do the trick:
FetchProfile fp = new FetchProfile();
fp.add("rfc822.text");
inbox.fetch(messages, fp);
If it is not possible to do this using Javamail, is it due to a constraint in the Javamail API or does the IMAP protocol simply not support this?
Limitation of JavaMail. The IMAP protocol allows fetching the bodies of several messages at once:
a1 fetch 1:* (rfc822.header rfc822.text)