Question

When I want to check who send a specific email to me with JavaMail.

I can use

Message.getFrom()

which returns an

Message[]

In what circumstances can a revived Message have multiple Identities it came from?

Regarding the API this makes more sense for outgoing Emails.

So can I really on, that:

Address from = message.getFrom()[0];

Always gives me back exactly 1 Address which the Message was send from?

I have now Implemented something like:

Address[] fromAddress = message.getFrom();

if (fromAddress == null || fromAddress.length > 1) {
    // Don't Process the Email and Notify me
}
Was it helpful?

Solution

Yes, it seems that Message.getFrom() can really return multiple addresses.

I had a look at the sources for Apache Geronimo's implementation of the JavaMail API, and it will return multiple addresses if there are multiple From: headers, or multiple addresses inside one header.

As to whether this could happen:

As explained by Alex K.'s answer, the standard allows multiple "From" addresses if there is a single "Sender". I don't know whether anyone really sends mails with multiple "From" addresses, but it is standards-compliant.

Another situation which actually happens in practice:

Some spammers apparently send mail with multiple From: headers. This is not standards-compliant, but apparently some mail servers still accept the mail. This is apparently done to get past mail filters that filter by the From: address. The spammers include multiple addresses in the hope that a simple-minded filter will let the message pass if it finds one header with a "From"-address it likes.

So in summary: Yes, you should expect Message.getFrom() to return multiple addresses (or none at all). However, you probably don't need to expend a lot of energy for handling that case. Maybe you can even get away with just logging it as an error - that depends on your application.

OTHER TIPS

SMTP's MAIL FROM command allows only 1 address however the From: header (IMF Format) is not restricted to a single address

RFC 5322, 3.6.2. Originator Fields: The originator fields of a message consist of the from field, ... The from field consists of the field name "From" and a comma- separated list of one or more mailbox specifications.

(This works because the RFC requires a single sender header if there are multible froms)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top