Can a Message have multiple senders?
Asked Answered
J

2

13

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
}
Judon answered 31/1, 2014 at 12:46 Comment(3)
The documentation shows that it can return an empty array.Revisionist
This makes sense if you want to send a Message and haven't set it yet. But every incoming Message should have exactly one Identity it came from. I don't get why an Email/Message ever should have more than one From field...Judon
Zero and one happen in practice. More than one is a sure sign of spam these days (that was different in the previous millenium) so just make sure your code doesn't crash in that case and everything will be fine.Treillage
E
8

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.

Edp answered 31/1, 2014 at 13:31 Comment(0)
A
6

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)

Aardvark answered 31/1, 2014 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.