1

I'm trying to write a mail client that sends across mails as well as reads the incoming mails. My google efforts on this didn't yield results.

Is there a way to attach a tag ( for identification ) to the mail envelope ( other than the subject) while sending the mail so that when a reply is done for the same mail, the code could recognize it?

FYI I'm using the java mail components for writing the client.

Thanks in advance..

Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
Symphony
  • 89
  • 1
  • 10
  • You can add a custom header field: http://stackoverflow.com/questions/2115342/create-custom-mail-header-field –  Feb 27 '12 at 19:23

3 Answers3

2

As Bill Shannon notes, arbitrary message headers would not be included in the reply. Realistically, the subject may be fundamentally similar in a reply. It will often be a prefixed version (for example, with "Re:") of the subject of the original message. Users, of course, can edit the subject, and broken mailers may do all sorts of crazy things.

You should be able to correlate a message reply with the original message by use of the References header on the reply. This header should contain RFC 2822, as its last element, the message ID of the message that is being replied to. You can, in theory, even track multiple replies back to the original message, as References should build up such that you can build the entire thread from the message ID list.

That is to say, when you send your original message, you should have a field:

Message-ID: <unique-message-id>

and in the reply, you should see:

References: <unique-message-id>

If, for some reason, you wish to use a different unique ID to track on, different from the message ID, you could also put that in your references list:

Message-ID: <unique-message-id>
References: <other-id>

and in replies, you should see:

References: <other-id> <unique-message-id>

Note that I said "should" repeatedly. This behavior was only codified in RFC2822, the behavior of the References header is poorly defined in the original RFC822. While (presumably) most mail clients follow RFC2822, you can see a list of (hopefully mostly outdated) caveats explained by jwz regarding the threading algorithm in Netscape's mail reader.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
1

The only header item you can depend on in the reply is the address, so if you want to track replies with perfect reliability you would need a different address for each message you send.

Usually the reply will include the original Subject, so that's the simplest place to put a tag.

Arbitrary message headers are typically not included in a reply.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • 1
    I've upvoted both of these answers. Although technically Message-ID and References headers can be of use, our company recommends to all of our customers that if you want a method you can truly rely on then use the to address. – Steve Smith Feb 28 '12 at 21:38
  • @SteveSmith: as in, sending from a unique from address for every message? (ie "From: foo+uniqueid@domain.com") That would indeed seem to be the most sensible approach. – Edward Thomson Feb 28 '12 at 22:32
  • @EdwardThomson exactly. That way the user just replies, if the from address is something like "Your Name " and has a friendly name too then they user will likely never even notice. – Steve Smith Feb 29 '12 at 16:00
  • @Thomson, Thank you for your suggestion. Unfortunately, I'm doing this email client for the first time, so some of the concepts you had mentioned just flew over my head ( although I tried to get answers from googling)sorry for my ignorance. I have currently written a client for reading and sending ( with attachment) mails using JavaMail. But I'm not able to add a "Tag" to the Reference header, any code snippet would help. Again thank you for your time and patience. – Symphony Feb 29 '12 at 17:46
  • You don't get the "tag" the Reference header. You get to set the Message-ID header on the message you send (or use the one JavaMail will generate for you). Then, in the reply message you get, you look for your Message-ID in the References header. – Bill Shannon Feb 29 '12 at 23:28
  • Thank You Shannon & Thomson:). – Symphony Mar 02 '12 at 17:52
0

Ok, is true that you can use In-Reply-To and References, but for example References is not allways used, for example I have clients that use Zimbra the VMware email system and that system take any References header away. And if you are trying to relay on In-Reply-To the problem is that for each recipient you will have the same In-Reply-To, and you can use the original email address to know who is answer an email because people do use email aliases, so if you send an email to say j@example.com he may replay you back from jhon@example.com which is his original email address.

So, Finally you can't relay on In-Reply-To or References, but what you can do is to use them to at list identify the thread, and try to validate with the sender email, or from email, and also you can try to add on the HTML part of the email (which at this point most email client has) a hidden tag search for the original recipient of this message been returned.

Event this complex testing wont get everything but probably something like 95% of the cases.

About using the reply-to as an alias of the account that's a nice approach, it should work in most cases, the only drawback is to use the "Your Name " approach that requires to you actually knowing the "Your Name" and that is not so easy in some cases.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
  • About sending a Message-Id different for each recipient, the problem with that is that in fact those email wont share the same thread, which in some cases it may be useful for you system. – Marcelo Marmol Jul 24 '12 at 19:29