2

我们有套接字应用程序,它会发送相当多的电子邮件。所以我们决定向其中发送大量消息,这将触发电子邮件。最终,我们看到电子邮件需要几个小时才能到达任何收件箱,无论是 gmail、hotmail 还是 yahoo 等。我们一开始就有这个代码。

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();
       }     
     }
}

那么基于此链接如何有效地使用 javax.mail API 发送批量邮件?& 我们可以使用重用认证会话来提高速度吗?我们试图改变它如下。但最终会出现邮件异常。我们尝试只构建一个会话并继续重复使用,以避免邮件传递延迟。我们在顶部声明这个 Session session = null; 存储创建的会话?

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 t = session.getTransport();
                        t.connect();
                        t.sendMessage(emailMessage, emailMessage.getAllRecipients());                       }
                   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.getInstance(props,new javax.mail.Authenticator()
              {             
                protected PasswordAuthentication getPasswordAuthentication() 
                {                   
                return new PasswordAuthentication("*********","*******");               
                }       
              });

   }

堆栈跟踪如下。

javax.mail.NoSuchProviderException: Invalid protocol: null
at javax.mail.Session.getProvider(Session.java:440)
at javax.mail.Session.getTransport(Session.java:659)
at javax.mail.Session.getTransport(Session.java:640)
at javax.mail.Session.getTransport(Session.java:626)
at commuSe $ConnectionHandler.sendEmail(commuSe .java:26028)
at commuSe $ConnectionHandler.run(commuSe .java:4734)
at java.lang.Thread.run(Thread.java:722)  
4

1 回答 1

6

您至少犯了两个常见的 JavaMail 错误。修复它们,看看是否有帮助。如果没有,请使用新代码和您收到的异常的详细信息(包括堆栈跟踪)更新您的帖子。

于 2013-07-11T23:37:42.420 回答