[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Is there anyway to tell javamail to ignore bad emails addresses and send message?

Posted on 2002-07-16
2
Medium Priority
?
1,718 Views
Last Modified: 2013-11-24
Hello,

Is there anyway to tell javamail to ignore bad emails addresses and send message?

Seems as though the whole message sending fails if one of the addressees is invalid...

javax.mail.SendFailedException: Sending failed;  nested exception is:      javax.mail.SendFailedException: Invalid Addresses;  nested exception is:      javax.mail.SendFailedException: 550 5.1.1 <bogusUser@test.com>... User unknown
Keith

0
Comment
Question by:keithedward
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
flumpman earned 400 total points
ID: 7159731
As far as I am aware, JavaMail is simply relaying a message back from the mail server.  So the invalid email addresses need to be removed before the message can be sent.

The code below removes the invalid email addresses and also the sent email addresses (don't want to resend the email to everybody if it partially succeeded).  Note that it can't send the message if there is no valid "to" address.

The code is a bit rough and would be a prime candidate for some refactoring, but you get the gist of what is happening.

--
flumpman



        } catch (SendFailedException e) {

            // get the addresses from the exception
            Address[] sent = e.getValidSentAddresses();
            Address[] unsent = e.getValidUnsentAddresses();
            Address[] invalid = e.getInvalidAddresses();

            // create ArrayLists to hold the addresses
            ArrayList toAddresses = new ArrayList();
            ArrayList ccAddresses = new ArrayList();
            ArrayList bccAddresses = new ArrayList();

            // copy the addresses from the Address arrays into the ArrayLists
            try {
                if (msg.getRecipients(MimeMessage.RecipientType.TO) != null)
                    toAddresses =
                        (ArrayList) new ArrayList(Arrays
                            .asList(msg.getRecipients(MimeMessage.RecipientType.TO)))
                            .clone();

                if (msg.getRecipients(MimeMessage.RecipientType.CC) != null)
                    ccAddresses =
                        (ArrayList) new ArrayList(Arrays
                            .asList(msg.getRecipients(MimeMessage.RecipientType.CC)))
                            .clone();

                if (msg.getRecipients(MimeMessage.RecipientType.BCC) != null)
                    bccAddresses =
                        (ArrayList) new ArrayList(Arrays
                            .asList(msg.getRecipients(MimeMessage.RecipientType.BCC)))
                            .clone();
            } catch (Exception ex) {
                ex.printStackTrace();
                return;
            }

            // remove the invalid addresses
            if (invalid != null) {
                for (int i = 0; i < invalid.length; i++) {
                    System.out.println("invalid address " + invalid[i]);
                    toAddresses.remove(invalid[i]);
                    ccAddresses.remove(invalid[i]);
                    bccAddresses.remove(invalid[i]);
                }
            }

            // remove the addresses the email was sent to
            if (sent != null) {
                for (int i = 0; i < sent.length; i++) {
                    System.out.println("sent to address " + sent[i]);
                    toAddresses.remove(sent[i]);
                    ccAddresses.remove(sent[i]);
                    bccAddresses.remove(sent[i]);
                }
            }

            // cannot re-send the email if there are no "to" addresses
            if (toAddresses.isEmpty()) {
                System.out.println("cannot send message without a valid \"to\" address");
                return;
            }

            System.out.println("**********************");
            System.out.println("resending message");

            // temporary object array to store the addresses
            Object[] addressArray;

            // copy the "to" addresses into an array of Addresses
            addressArray = toAddresses.toArray();
            Address[] toAdds = new Address[addressArray.length];
            System.arraycopy(addressArray, 0, toAdds, 0, addressArray.length);

            // copy the "cc" addresses into an array of Addresses
            addressArray = ccAddresses.toArray();
            Address[] ccAdds = new Address[addressArray.length];
            System.arraycopy(addressArray, 0, ccAdds, 0, addressArray.length);

            // copy the "bcc" addresses into an array of addresses
            addressArray = bccAddresses.toArray();
            Address[] bccAdds = new Address[addressArray.length];
            System.arraycopy(addressArray, 0, bccAdds, 0, addressArray.length);

            try {
                // set the message recipients
                msg.setRecipients(MimeMessage.RecipientType.TO, toAdds);
                msg.setRecipients(MimeMessage.RecipientType.CC, ccAdds);
                msg.setRecipients(MimeMessage.RecipientType.BCC, bccAdds);

                // print out the to recipients                        
                System.out.println("**********************");
                Address[] tmp = msg.getRecipients(MimeMessage.RecipientType.TO);

                for (int i = 0; i < tmp.length; i++)
                    System.out.println("to: " + tmp[i]);

                // print out the cc recipients
                tmp = msg.getRecipients(MimeMessage.RecipientType.CC);
                for (int i = 0; tmp != null && i < tmp.length; i++)
                    System.out.println("cc: " + tmp[i]);

                // print out the bcc recipients
                tmp = msg.getRecipients(MimeMessage.RecipientType.BCC);
                for (int i = 0; tmp != null && i < tmp.length; i++)
                    System.out.println("bcc: " + tmp[i]);

                // send the message                        
                Transport.send(msg);
            } catch (MessagingException ex) {
                ex.printStackTrace();
            }
0
 

Author Comment

by:keithedward
ID: 7159924
Thanks!
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question