Solved

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

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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

696 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