newbie14 0 Posting Pro

We have socket application which sends out quite a number of email out. So we decided to send huge number message into it which will trigger emails. Eventually we see the email are taking hours before it reach any of the inboxes either gmail,hotmail or yahoo etc. We have this codes in the begining.

public class commuSe {
  BoneCP connectionPool = null;
  class ConnectionHandler implements Runnable {

   private Socket receivedSocketConn1;
    ConnectionHandler(Socket receivedSocketConn1) {
      this.receivedSocketConn1=receivedSocketConn1;
    }
    public void run() {
     .....
    }
    void sendClientEmail(String emailMessageString)
     {
      try 
      {
          Properties props = new Properties();      
          props.put("mail.smtp.host", "**********");        
          props.put("mail.smtp.socketFactory.port", "******");      
          //props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");        
          props.put("mail.smtp.auth", "true");      
          props.put("mail.smtp.port", "*****");         
          Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
          {             
            protected PasswordAuthentication getPasswordAuthentication() 
            {                   
            return new PasswordAuthentication("*********","*******");               
            }       
          });
            int count=0;
            System.out.println("\n\nClient Email queue took ready :"+emailMessageString);
               try 
               {
                    String[] eMArray = null;
                    eMArray = emailMessageString.split("@EmL@");
                    Message emailMessage = new MimeMessage(session);            
                    emailMessage.setFrom(new InternetAddress("****************"));
                    if(eMArray.length>1)
                    {
                     for(int iEmail=1; iEmail<eMArray.length ; iEmail++)
                     {
                      String cc1 = eMArray[iEmail];
                      emailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(cc1));
                     }                          
                     emailMessage.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("**************"));
                    }
                    else
                    {
                     emailMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse("*************"));   
                    }
                    emailMessage.setSubject("Alerts");
                    emailMessage.setText(eMArray[0]);
                    Transport.send(emailMessage);
               }
               catch (Exception e) 
               {
               System.out.println("Transport Problem");
               e.printStackTrace();
               } 
       }
        catch (Exception e) 
       {
        System.out.println("Main email try got problem");
        e.printStackTrace();
       }     
     }
}

So based on this link How to Send bulk mails using javax.mail API efficiently? & Can we use reuse authenticated sessions to improve speed? we tried to change it as following. But end up with the mail exception. We tried to build just one session and keep reusing so avoid the mail delivery delay. We declare this at the top Session session = null; to store the session created?

public class commuSe {
      BoneCP connectionPool = null;
       Session session = null;
      class ConnectionHandler implements Runnable {

       private Socket receivedSocketConn1;
        ConnectionHandler(Socket receivedSocketConn1) {
          this.receivedSocketConn1=receivedSocketConn1;
        }
        public void run() {
         .....
        }
        void sendClientEmail(String emailMessageString)
         {
          try 
          {
                              int count=0;
                System.out.println("\n\nClient Email queue took ready :"+emailMessageString);
                   try 
                   {
                        String[] eMArray = null;
                        eMArray = emailMessageString.split("@EmL@");
                        Message emailMessage = new MimeMessage(session);            
                        emailMessage.setFrom(new InternetAddress("****************"));
                        if(eMArray.length>1)
                        {
                         for(int iEmail=1; iEmail<eMArray.length ; iEmail++)
                         {
                          String cc1 = eMArray[iEmail];
                          emailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(cc1));
                         }                          
                         emailMessage.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("**************"));
                        }
                        else
                        {
                         emailMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse("*************"));   
                        }
                        emailMessage.setSubject("Alerts");
                        emailMessage.setText(eMArray[0]);
                        Transport.send(emailMessage);
                   }
                   catch (Exception e) 
                   {
                   System.out.println("Transport Problem");
                   e.printStackTrace();
                   } 
           }
            catch (Exception e) 
           {
            System.out.println("Main email try got problem");
            e.printStackTrace();
           }     
         }
    }
     public static void main(String[] args) {
     new commuSe ();
   }
   commuSe () {
     Properties props = new Properties();      
              props.put("mail.smtp.host", "**********");        
              props.put("mail.smtp.socketFactory.port", "******");      
              //props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");        
              props.put("mail.smtp.auth", "true");      
              props.put("mail.smtp.port", "*****");         
              session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
              {             
                protected PasswordAuthentication getPasswordAuthentication() 
                {                   
                return new PasswordAuthentication("*********","*******");               
                }       
              });

   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.