/* * [Comb.java] * * Summary: Combs a list of email addresses and returns a list of valid ones, logging bad ones. * * Copyright: (c) 2002-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.7 2007-08-21 */ package com.mindprod.bulk; import com.mindprod.csv.CSVReader; import javax.mail.internet.InternetAddress; import java.io.EOFException; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.Date; import static com.mindprod.bulk.CustConfig.CUST_NAME; import static com.mindprod.bulk.CustConfig.EMAIL_ADDRESS_QUALITY; import static com.mindprod.bulk.CustConfig.MAX_EMAILS_IN_BATCH; import static com.mindprod.bulk.CustConfig.MONITORS; import static java.lang.System.*; /** * Combs a list of email addresses and returns a list of valid ones, logging bad ones. * * @author Roedy Green, Canadian Mind Products * @version 1.7 2007-08-21 * @since 2002 */ final class Comb { /** * Combs a list of email addresses and returns a list of deliverable ones, logging bad ones. It works via * CachedEmailProber ensuring Email servers are working. * * @param ir source of email addresses, CSV, comma delimited, four fields, email, dear, fullname, account Typically * ByteArrayInputStream attachment or FileInputStream abundance emails.txt export. * @param log Where to log the errors. * * @return ArrayList of InternetAddresses of deliverable emails. */ @SuppressWarnings( { "InfiniteLoopStatement", "SameParameterValue", "QuestionableName" } ) public static ArrayList comb( Reader ir, Log log ) { try { final ArrayList result = new ArrayList<>( MAX_EMAILS_IN_BATCH + MONITORS .length + 1 ); final CSVReader r = new CSVReader( ir ); int good = 0; int bad = 0; try { log.println( "" ); log.println( new Date().toString() + " " + CUST_NAME ); while ( true ) { String email = r.get(); // ignore empty lines especially at end of file. if ( email == null ) { continue; } String dear = r.get(); String fullname = r.get(); String acct = r.get(); r.skipToNextLine(); // don't echo to log out.println( " checking " + email ); // higher the number better it is 0..9 final int howGood = EmailSyntaxValidator.howValid( email ); if ( howGood > EMAIL_ADDRESS_QUALITY ) { if ( CachedMailProber.isMailServerForEmailAlive( email ) ) { result.add( new InternetAddress( email, /* human */ dear ) ); good++; } else { log.println( ">>>recipient's mail server not working. goodness:" + howGood + " " + email + "," + dear + "," + fullname + "," + acct ); bad++; } } else { /* don't even bother probing the really hopeless stuff */ log.println( ">>>recipient rejected based on form of address. goodness:" + howGood + " " + email + "," + dear + "," + fullname + "," + acct ); bad++; } } // end while } catch ( EOFException e ) { log.println( good + " emails passed inspection, " + bad + " emails rejected." ); log.println( "" ); r.close(); // don't close. We will need it again for the next message. CachedMailProber.flush(); return result; } } catch ( IOException e ) { err.println(); e.printStackTrace( err ); err.println(); log.println( "Likely malformed list of emails" ); return null; } } // end comb } // end Comb