Solved

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

Posted on 2002-07-16
2
1,351 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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now