• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1765
  • Last Modified:

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

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
keithedward
Asked:
keithedward
1 Solution
 
flumpmanCommented:
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
 
keithedwardAuthor Commented:
Thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now