Content ID for mail parts - java

What is the importance of Content ID in mail? Is there any difference in the way this value is set across the most popular mail clients say Thunderbird, Microsoft Outlook and so...
What are things a developer should have in mind while providing a service that is related to reading mails sent from different clients to provide cross client compatibility, specifically with Content ID?
Thanks and Regards,

The first thing to keep in mind is that the Content-ID header for a body part is optional. It's mostly only needed for multipart/related parts.
You should read RFC 2045, which defines the Content-ID header.
What were you planning to use the Content ID for?

Related

Can an email contain multiple Message Ids?

I am implementing a mail client in java and I am retrieving the MessageId using the command: String[] msgIds = msg.getHeader("Message-Id");
Since getHeader() returns an Array. I was wondering if there is any scenario where an email might contain multiple Ids.
I tried testing it by sending/replying/forwarding an email back and forth but it only contained one id every time.
The current specification for internet email message format is RFC 5322. That specifies that an email message can have zero or one "message-id" headers, and that one is recommended. (See page 20 in the linked version)
So any email that has more than one "message-id" header is non-conformant.
However, if you are implementing a mail reader or processor, it is advisable to allow for the possibility of a non-conformant email message. At the very least, your processor should cope with such an email so that it doesn't crash or behave in a destructive fashion. (That kind of fragility could allow someone to attack your mail processor, and maybe the system that it runs on.)
An E-Mail might not contain multiple Message-Id headers but other E-Mail header fields might appear multiple times. The getHeader function is returning an array to take these into account.
For example the Recieved header can be set multiple times to provide a full trace of servers that handled the E-Mail.

IMAP, tracking moved messages using message id

We are developing a mail client written in Java. It has same functionalities like Outlook or Thunderbird, etc. It communicates with the mail server directly. Additionally, our business rules demand that we store all messages in our database and messages should be kept synchronised always. I know that is not very suitable for IMAP, but we must keep everything in our database.
Question arises, how to track an IMAP message moved from folder A to folder B? How can we get informed about that? If you remove a message from A, it is deleted from A and it created newly in B, as a result: The UID value of the message is changed. Can we rely on the MessageID found in the headers? I checked some mails servers and see that the message id in the headers remain unchanged. But i have read somewhere, that the messageids can be empty depending on the mail server.
Are the MessageID in headers always set, can be cases or mailservers that they leave it blank?
Are the MessageID value in headers unique in an IMAP folder?
Is it possible that it gets changed when message is moved or folders UIDVALIDITY changed?
What about setting a custom header during fetch? When I add a non-standart header name value pair, will it be kept on the mail server or is it possible that non-standart mail heraders will be deleted by mail server? Is it a bad idea applying a non-standart header value?
IMAPMessage m;
m.setHeader("myHeader", "myValue");
There were some suggestions in stackoverflow, it is said to generate a hash including messageId and other parameters such as sender, subject etc, is it a safe approach? We can get conflicts if there is no unique MessageID is provided or no MessageID is provided.
There are three things you can do.
First, message-id. You can rely on the message-id being present and unique these days if your mode of failure is good enough. In your case, if the message-id is not there and a message is moved, is the failure just that you waste space in the database and/or download the message twice? The wasted space will be small these days.
Second, x-gm-msgid. That's a gmail-specific feature, a 63-bit number that never changes. If two messages have the same x-gm-msgid, they are the same.
Third, the COPYUID response code tells you about moves, but only applies when you do the moving, not when someone else does.
Put together, these should give you a fairly good understanding of how the user's mailboxes change.

How to add a flag to existing POST api

I have a REST api built for creating orders. The behavior is such that the person who creates an order received an email back saying "You created an order XXX". This email is triggered all the time.
The api appears like this
http://api.mytestevnt.com/ordering/orders - POST with request body as the order entity json.
Now i want to give a feature to the api caller to indicate if the email notification is necessary or not. What's the best way to do this?
I think it depends on whether email notification is data or metadata. If it's part of the order, then definitely add it to the request body. If it's metadata, you have two choices. If you think there will be lots of metadata, you can either edit the order to have a metadata section or you can POST the metadata separately. If there will only be a limited amount of metadata, I would suggest using a query parameter.
You should avoid using a header unless you control the entire path from the client to the server, because proxies or load balancers are allowed to strip non-standard headers.
Include in the POST body a send_email=1 or send_email=0 param. You'll extract that, and see what the user wants to do.
Search "how to get POST variables in JAVA".
Accessing post variables using Java Servlets
You can do like this:
Add a new Java attribute(like boolean emailEnabled) in your Java Request Object for your REST service.
Client side which invokes your REST service need to provide that parameter you added in your server side, you can set a default value for that too.

Java Chat system protocol design, how to determine message type?

I have a chat program implemented in Java. The client can send lots of different types of information to the server (i.e, Joins the server and sends username, password; requests a private chat with another user on the server, disconnects from the server, etc).
I'm looking for the correct way to have the server/client differentiate between 'text' messages that are just meant to be chat text messages sent from one client to the others, and 'command' messages (disconnect, request private chat, request file transfer, etc) that are meant for the server or the client.
I see two options:
Use serialized objects, and determine what they are on the receiving end by doing an 'instanceof'
Send the data as a byte array, reserving the first N bytes of the array to specify the 'type' of the incoming data.
What is the 'correct' way to do this? How to real protocols (oscar, irc) handle this situation?
I've googled around on this topic and only found examples/discussions centering on simple java chat applications. None that go into detail about protocol design (which I ultimately intend to practice).
Thanks to any help...
Second approach is much better, because serialization is a complex mechanism, that can be easily used in a wrong way (for example you may bind yourself to internal content of a concrete serialized class). Plus your protocol will be bound to JVM mechanism.
Using some "protocol header" for message differentiation is a common way in network protocols (FTP, HTTP, etc). It is even better when it is in a text form (people will be able to read it).
You typically have a little message header identifying the type of content in all messages, including standard text/chat messages.
Either of your two suggestions are fine. (In your second approach, you probably want to reserve some bytes for the length of the array as well.)

how do you set session attributes with java.net.URLConnection?

how do you set session attributes with java.net.URLConnection or its subclasses?
In other words, what is the equivalent of HttpSession.setAttribute(String, Object) in Java.net.URLConnection? URLConnection does have setRequestProperty(String, String), but it does not help me as I need similar functionality as session.setAttribute(String, Object) for setting a timestamp.
In other words, how do you express following call with URLConnection?
HttpSession.setAttribute("timestamp", timestamp);
I cant change the contract with the third party, that is my request must have an timestamp attribute with a java.util.Date value.
Any help appreciated.
You can't. The session is kept, privately, at server-side, and is not available from the client. It's not part of the HTTP protocol at all, but is just something that nearly all server-side webapp APIs offer to allow keeping state associated to one particular user of the application.
Basically, what you're asking is the equivalent of "what can I put in my email to store some text in the C:\session.txt file on the hard drive of the recipient?":
there is perhaps no C:\session.txt file
you can't access the hard drive of a recipient by writing something in an email. It would constitute a big security problem

Categories

Resources