Solved

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

Posted on 2002-07-16
2
1,419 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
2 Comments
 
LVL 2

Accepted Solution

by:
flumpman earned 100 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPSessionBindingListernter not configured in deployment descriptor 2 65
oracle 11g 23 107
tomcat not starting 6 68
login jsp example 24 67
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

839 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