I am using javax.mail system and have problems with "Invalid address" exceptions. Here's the basics of the code:
// Get system properties Properties props = System.getProperties(); // Setup mail server props.put("mail.smtp.host", m_sending_host); // Get session Session session = Session.getDefaultInstance(props, new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(m_sending_user, m_sending_pass); } }); // Define message MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(m_sending_from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(vcea.get(i).emailaddr)); message.setSubject( replaceEnvVars(subject) ); message.setText(replaceEnvVars(body)); // Send message try { Transport.send(message); } catch (Exception e){ Log.Error("Error sending e-mail to addr (%s): %s", vcea.get(i).emailaddr, e.getLocalizedMessage() ); }
The problem is that the above code really works. But for some email addresses that I know are valid (because I can send them through a standard email client), the code above will throw an "Invalid Address" exception when trying to send.
Any hints or tips are appreciated.
- Update: authentication problem.
Ok, here I discovered that this is happening. Upon receipt of the email, the code above sets authentication correctly and the Authenticator.getPasswordAuthentication () callback is actually called.
Not so when sending emails. You have to do a little more. Add this:
// Setup mail server props.put("mail.smtp.host", m_sending_host); props.put("mail.smtp.auth", "true");
which will force the javax.mail API to authenticate the login. And then use the actual transport instance instead of the static .send () method:
Transport t = session.getTransport(m_sending_protocol); t.connect(m_sending_user, m_sending_pass);
...
// Send message try { t.sendMessage(message, message.getAllRecipients()); } catch (Exception e){
Without forced authentication, the mail server saw me as an unauthorized relay and simply shut me down. The difference between the addresses that "worked" and not the addresses that were not the same were those that "worked" were local to the mail server. Therefore, he simply accepted them. But for any non-local "relay" addresses, it would reject the message because my authentication information was not provided by the javax.mail API when I thought it would be.
Thanks for the tips to encourage me to also look at the mail server side.