A mail server returns this error when I send mail with several recipients :
Remote Server returned 'mx.spamexperts.com #5.0.0 smtp; 550 Messages should have one or no To headers, not 5.'
It happens when I use addRecipient
method of JavaMail multiple times. If I use setRecipient
instead with an array of email address, it works.
This is the consequence of the addHeader
method in javax.mail.internet.InternetHeaders
class. Here is the piece of code which is the cause of this issue :
for (int i = headers.size() - 1; i >= 0; i--) {
InternetHeader h = (InternetHeader)headers.get(i);
if (name.equalsIgnoreCase(h.getName())) {
if (addReverse) {
pos = i;
} else {
headers.add(i + 1, new InternetHeader(name, value));
return;
}
}
// marker for default place to add new headers
if (h.getName().equals(":"))
pos = i;
}
What do you think about that ? Why JavaMail adds new To
header each time we use addRecipient
if some mail server does not accept it ?
You must be using a very old version of JavaMail. This was fixed in JavaMail 1.4.4, over 5 years ago. The current version is 1.5.5.