0

I have the following code

boolean debug = true;

// Set the host smtp address
Properties props = new Properties();
props.put( "mail.smtp.host", "xxxxx" );


// Create some properties and get the default Session
Session session = Session.getDefaultInstance( props, new Authenticator(){
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("preddy@indsci.com", "<PWD>");
    }
});


session.setDebug( debug );



// Create a message
MimeMessage message_= new MimeMessage( session );

message_.setSubject( "Test Sub");
message_.setContent( "Hello", "text/plain" );


InternetAddress from = new InternetAddress( "xxxx<yyyy@zzzz>" );
InternetAddress replyTo = new InternetAddress( "xxxx<yyyy@zzzz>" );

  InternetAddress[] to = new InternetAddress[3];
  to[0] = new InternetAddress( "valid addredd" ); 
  to[1] = new InternetAddress( "Wrong Domain email" );
  to[2] = new InternetAddress( "Prasad Wrong Email " );


  try
  {
     Transport.send( message_ );
     System.out.println("*********************** No exception ");
  }
   catch (SendFailedException ex)
  {
  // Build the list of invalid addresses.
  List<Address> invalidEmailList = new ArrayList<Address>();

  // first, get the email list from the exception
  if (ex.getInvalidAddresses() != null
      && ex.getInvalidAddresses().length > 0)
  {
    invalidEmailList.addAll( Arrays.asList( ex.getInvalidAddresses() ) );
  }

  if (ex.getValidUnsentAddresses() != null
          && ex.getValidUnsentAddresses().length > 0)
      {
        invalidEmailList.addAll( Arrays.asList( ex.getValidUnsentAddresses()      ) );
      }

    // then tack on the invalid email addresses we passed in.
    if (invalidAddresses_.size() > 0)
    {
      invalidEmailList.addAll( invalidAddresses_ );
    }

    System.out.println(invalidEmailList);

    ex.printStackTrace();

  // Throw the exception up to higher handlers
  //throw ex;

  } catch(javax.mail.MessagingException mx) {

    System.out.println(mx.getMessage());

  } catch (Exception ex) {

    System.out.println(ex.getMessage());

} 

`

If I try above code with only valid email, it is sending email but when I start include others wrong domain and invalid email addresses, all emails are failing. Here is the debug info

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning            javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "xxxxx", port 25, isSSL   false
220 xxxxxx Microsoft ESMTP MAIL Service ready at Wed, 30     Mar 2016 05:36:58 -0400
DEBUG SMTP: connected to host "XXXXXXX", port: 25


250-xxxxxxx- Hello [172.17.36.139]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM LOGIN
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250-XRDST
250 XSHADOW
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "NTLM LOGIN"
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "XEXCH50", arg ""
DEBUG SMTP: Found extension "XRDST", arg ""
DEBUG SMTP: Found extension "XSHADOW", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<preddy@predictivesolutions.com>
250 2.1.0 Sender OK
RCPT TO:<preddy@indsci.com>
250 2.1.5 Recipient OK
RCPT TO:<preddy@indsci1.com>
550 5.7.1 Unable to relay
RCPT TO:<preddy123@indsci.com>
250 2.1.5 Recipient OK
DEBUG SMTP: Valid Unsent Addresses
DEBUG SMTP:   Prasad Reddy <preddy@indsci.com>
DEBUG SMTP:   Prasad Wrong Email <preddy123@indsci.com>
DEBUG SMTP: Invalid Addresses
DEBUG SMTP:   Prasad Wrong Domain <preddy@indsci1.com>
DEBUG SMTP: Sending failed because of invalid destination addresses
RSET
250 2.0.0 Resetting
DEBUG SMTP: MessagingException while sending, THROW: 
 javax.mail.SendFailedException: Invalid Addresses;
 nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1862)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.dbo2.pipeline.util.MailHandler.sendMessage(MailHandler.java:583)
at     com.dbo2.pipeline.util.MailHandler.postMailImplementation(MailHandler.java:321)
at com.dbo2.pipeline.util.MailHandler.postMail(MailHandler.java:165)
at com.dbo2.pipeline.util.MailHandler.main(MailHandler.java:568)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to    relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1715)
... 7 more
QUIT
221 2.0.0 Service closing transmission channel
   [Prasad Wrong Domain <preddy@indsci1.com>, Prasad Reddy <preddy@indsci.com>,       Prasad Wrong Email <preddy123@indsci.com>]
  *********************** Exception***********************
 javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1862)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.dbo2.pipeline.util.MailHandler.sendMessage(MailHandler.java:583)
at    com.dbo2.pipeline.util.MailHandler.postMailImplementation(MailHandler.java:321)
at com.dbo2.pipeline.util.MailHandler.postMail(MailHandler.java:165)
at com.dbo2.pipeline.util.MailHandler.main(MailHandler.java:568)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1715)
... 7 more

My requirement to send email to valid email address and flag all emails which are invalid with proper domain and also find out emils with wrong domain. The same code tested on another machine is not throwing exception at all

Any help is appreciated

1 Answers1

1

Before adding an email adress to your message. You have to check if it is a valid address. For example by using this regex:

^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)(\.[A-Za-z]{2,})$;

And one more:

"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$"

Which makes save that the address has the correct format.

And here an other post showing how to validate an email address: Java regex email

Community
  • 1
  • 1
Rene M.
  • 2,660
  • 15
  • 24
  • I have the code for validation already. All emails are valid. I have address like prasadreddykalki@gmail.com, which is valid email. I added other email addresses like prasadreddykalki@gmail123.com, prasadreddy12345678900000@gmail.com. All are valid email for validation. When I pass all above emails, all will fail. If I incluse the first one only then it is sending email – user1110622 Mar 30 '16 at 10:04
  • **It is not about basic validation** , but to find the invalid emails from SendFailedException – user1110622 Mar 30 '16 at 13:22
  • I used "mail.smtp.partialSend" property and then it starting sending email to valid emails. – user1110622 Mar 31 '16 at 18:20
  • **Guys, Any idea why it was not working on my friend's local machine?** – user1110622 Mar 31 '16 at 18:22