javax.mail.SendFailedException: Relaying denied

Hi Guys,

I am facing problem in using Java Mail apis. I am able to send mails within the domain but not able to send outside the domain. I get following exception when i try to send mail outside the domain.

javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for <someEmailAdress outside the domain>

Following is my code which I am using to send mail.

javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props, null);
javax.mail.Message msg = new javax.mail.internet.MimeMessage(mailSession);

InternetAddress addressFrom = new InternetAddress(sFromUser);
msg.setFrom(addressFrom);
msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo)
//addressTo is an array of InternetAddress.
//sFromUser is a Id with the domain.

When the addressTo array contains addresses in the same domain, it works fine but when it contains addresses outside domain like someaddress@gmail.com it give the above mentioned exception.

I know that my domain is configured in a way that it will not relay mails outside the domain but my problem is its ok if it doesn’t relay mails outside the domain but then it should send mails within the domain ans leave the others.

Currently if even a single Id is there which is out of domain it doesn’t send mail to any body anf throws the exception

Is there any way to achieve this. Please reply me as soon as possible.

Thanks,
-Ajay

Maybe, maybe not. In general I would think if an email can’t be sent by the email server to do errors in the request then the entire message should fail. If the server doesn’t do this, how would you know that some people were sent the message and some were not? You might end up sending the same email over and over again to some people.

There are a couple of solutions to your problem that popped to the top of my head.

1.) Send out only emails with one recipient. That way you avoid having to figure out which email address(es) caused the email not to be sent. To implement this, just loop through all your recipients and send them the mail.

2.) Figure out which email address(es) caused the email not to be sent and then remove them from the recipient list. I’m pretty sure that the exception thrown actually gives you the email address that caused the email to to be sent. You should be able to remove that email from your recipient list and then resend the email.

So, hopefully you’ve kind of written a wrapper around the standard Java Mail API, so that you have your own MailMessage and MailServer classes. If you don’t have the wrapper classes you might want to consider writing them or using a third party email implementation like spring’s or apache’s that is built on top of the standard java mail api.

Best of Luck.

Thanks for your quick reply hooknc!!..I agree your point is correct that the entire mail should fail. I was just expecting if we can achieve that.

Well I already tried mailing one recipient at a time but there are few problems with this approcah

  1. It is a bit slower.
  2. I am dealing with more then 1000 users in my domain. If out of those 50-60 Ids are incorrect in the domain, I will get 50-60 different mails from System Administrator regarding delivery failure.
    If I send mail at once to all the users I will get a single delivery failure mail containing 50-60 Ids.

Yes we can get the ID from the exception but point number 2 is not acceptable.

I think I wil have to write my own wrapper.
Keep posting to this thread hooknc if you have more inputs.

Thanks once again!!

-Ajay

I bet it won’t be too slow. It’s only a 1000 users. I bet your email server could send out a 1000 emails pretty quickly. I would at least try this technique since it’s so simple.

My guess is that the email is never being sent out so no failure notice is produced and returned to the sender (I could be wrong on this). So, your System Administrator will never be the wiser that something went wrong.

So, if you have a list/set of email address’ then you should be able to catch the exception iterate through all the bad emails and then resend. Meh, not the greatest idea, but it could work.

Best of Luck.

For the Ids which are incorrect in the domain its generating a mail like this:

someid1@domain.com on 1/7/2009 3:10 PM
The e-mail account does not exist at the organization this message was sent to. Check the e-mail address, or contact the recipient directly to find out the correct address.
<mail server ip #5.1.1>

someid2@domain.com on 1/7/2009 3:10 PM
The e-mail account does not exist at the organization this message was sent to. Check the e-mail address, or contact the recipient directly to find out the correct address.
<mail server ip #5.1.1>

If I put this in a loop I will get 2 diferent mails.
All this is happening for the IDs which are in domain but are incorrect and for the Ids which are outside the domain it will through an exception and will not send mail to any body.
Yes, I can refine my list of IDs and thn I can send mail.

Thanks,
-Ajay

So you have two different problems really.

1.) I have User email data that is outside of my domain. Which can be handled by attempting to send the email (either single emails or a bulk email).

2.) I have User email data that is inside my domain, but is wrong. Which is figured out only after the email message has been sent by the email server.

In problem 1 we can either still send out all emails individually with ignoring/logging the “javax.mail.SendFailedException: Invalid Addresses” exceptions or when sending a bulk mail, catching the “javax.mail.SendFailedException: Invalid Addresses” and removing those address’ from the bulk mail and trying again.

In problem 2, well, there is a problem. “How are we going to know that data in our database is messed up in this case?” I propose that you won’t know about that data until it’s too late and then the admin (whom ever that person is…) is going to have to correct the problem email address and either resend the email or wait until the next batch of emails goes out.

Note: The admin does not have to be a person it could be a system that attempts to correct the email address and then resend to that person, but normally it is better to have a human actually look at the broken address’ to figure out what the hell went wrong. But in the long run the chatter and wrong address’ in the system will be come small (hopefully).

Just my 2 cents.

And there could be other solutions out there, the above solutions are just possible ideas.

Email can be very difficult to use, but many systems have it as a requirement as a notification system.

Point of information…

If the email address’ are internal, but still incorrect, you most likely will receive one email per incorrect address. The same number of error emails will occur using the individual email solution or bulk email solution.

Well if we send mail in bulk then for the internal users we get only one single mail containing all the incorrect email addresses…

The only solution to problem is I will have to refine the email ID list that is i will have to seperate domain ones from the others and send the mail.

See the problem arises only when mail server is configured in a way so that it can not send mail out of domain if it capable of sending mails out of domain then no exception will come.

I think we will have to hardcode the domain or read it from a properties file by defining a custom property and thn just put a condition like

if(emailId.index(@domain.com)>0)
arrayList.add(emailId);

I just wanted to avoid this dependency so I was looking for a different solution.

What do you say??..Does it sounds good?..

Thanks,
-Ajay

yeah, i would try and avoid that type of programming too, but it might not be too bad if your design is good.

I would have an interface for what ever it is that will send your emails…

Create a class that implements that interface, but without any specialized code in it. Then you could create a subclass of that first class that does the above check for you by overloading one of the parents methods.

There are lots of ways to “hide” the implementation of checking the email address. The above idea was just the first one that came to my head. You could also look up the Strategy design pattern. That could work too.

Do you know if you have to report back on the email address’ that aren’t in your domain? That might change some of your design as well.

psst - 90% takes 10%…

coughhttp://en.wikipedia.org/wiki/Ninety-ninety_rule*cough*

:slight_smile:

Fair enough. Though I have you on a technically correct variant, having read the link, I like yours better :smiley: