javax.mail.NoSuchProviderException:无效的协议:null [英] javax.mail.NoSuchProviderException: Invalid protocol :null

查看:304
本文介绍了javax.mail.NoSuchProviderException:无效的协议:null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有套接字应用程序发出相当多的电子邮件。所以我们决定向它发送大量的消息,这将触发电子邮件。最终我们看到电子邮件需要几个小时才能到达任何收件箱gmail,hotmail或yahoo等。我们在开头有这个代码。

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

所以基于此链接< a href =https://stackoverflow.com/questions/13287515/how-to-send-bulk-mails-using-javax-mail-api-efficiently-can-we-use-reuse-auth>如何发送批量邮件有效使用javax.mail API? &安培;我们可以使用重用经过身份验证的会话来提高速度吗?我们尝试将其更改为以下内容。但最终会遇到邮件异常。我们尝试只构建一个会话并继续重用,以避免邮件传递延迟。我们在顶部Session session = null声明这个;存储创建的会话?

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 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)  


推荐答案

你做过这些常见的JavaMail错误中至少有两个。修复它们,看看是否有帮助。如果没有,请使用新代码和您获得的异常的详细信息更新您的帖子,包括堆栈跟踪。

You've made at least two of these common JavaMail mistakes. Fix them and see if that helps. If it doesn't, update your post with the new code and the details of the exception you're getting, including the stack trace.

这篇关于javax.mail.NoSuchProviderException:无效的协议:null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆