2010-12-15 202 views
1

我的網站基於JSP頁面和servlet。我在我的vps上運行Tomcat 6。使用JavaMail發送電子郵件,由於「連接太多」而導致「連接被拒絕」 - 爲什麼?

我使用JavaMail 1.4版在我的網站上發送電子郵件。

我可以在會議期間發送幾封電子郵件,但今天我在我的網站的提交頁面上找到了一個空白頁。所以,我看着Tomcat的日誌,這裏發生了什麼事 -

 
INFO | jvm 1 | 2010/12/15 10:29:13 | DEBUG: setDebug: JavaMail version 1.4.1 
INFO | jvm 1 | 2010/12/15 10:29:13 | DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 
INFO | jvm 1 | 2010/12/15 10:29:13 | DEBUG SMTP: useEhlo true, useAuth true 
INFO | jvm 1 | 2010/12/15 10:29:13 | DEBUG SMTP: trying to connect to host "smtp.myisprovider.net", port 25, isSSL false 
INFO | jvm 1 | 2010/12/15 10:29:14 | 421 4.7.1 - Connection Refused - - Too many connections 
INFO | jvm 1 | 2010/12/15 10:29:14 | DEBUG SMTP: could not connect to host "smtp.myisprovider.net", port: 25, response: 421 
INFO | jvm 1 | 2010/12/15 10:29:14 | 
INFO | jvm 1 | 2010/12/15 10:29:14 | javax.mail.MessagingException: Could not connect to SMTP host: smtp.myisprovider.net, port: 25, response: 421 
INFO | jvm 1 | 2010/12/15 10:29:14 | at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1379) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at javax.mail.Service.connect(Service.java:288) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at javax.mail.Service.connect(Service.java:169) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at spyder.servlets.email.MessageCenterServlet.cancelService(MessageCenterServlet.java:836) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at spyder.servlets.email.MessageCenterServlet.doPost(MessageCenterServlet.java:172) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at spyder.servlets.email.MessageCenterServlet.doGet(MessageCenterServlet.java:39) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:774) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:703) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:896) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690) 
INFO | jvm 1 | 2010/12/15 10:29:14 | at java.lang.Thread.run(Thread.java:595) 

我通過我的ISP發送提供我的電子郵件。我在家使用互聯網的人。這是而不是與我的VPS運行相同的提供程序。

因此,這個問題是我的ISP提供商的結果,無論是在他們的最終設置,還是這與我的代碼在我的servlet?

日誌說:「太多的連接」,所以最初我想知道如果這是我的結束東西,如果有什麼我應該關閉(在servlet,調用close()方法在發送郵件之後?)?

編輯的帖子,包括從servlet的片段...


Transport t = null; 
      try {     

        //get email address to send 
        //this email to 
        rs = stmt.executeQuery("Select Tech_Support_Email,POP3,SMTP from sitewide_info");     

        String toName = "mywebsite.com"; 
        String host_email_address = ""; 
        String POP3 = ""; 
        String SMTP = ""; 

        if(rs.next()) { 
         host_email_address = rs.getString(1); 
         POP3 = rs.getString(2); 
         SMTP = rs.getString(3); 
        }     

        // Specify the SMTP Host 
         Properties props = new Properties();           

        //props.put(POP3, SMTP); 
        props.setProperty("mail.smtp.auth", "true");         
        props.put("mail.pop3.host", POP3); 
       props.put("mail.smtp.host", SMTP); 

       // Create a mail session 
        Session ssn = Session.getInstance(props, null); 
        ssn.setDebug(true);      

       //... 
       //query db for some information, that will be appended to email    
       //... 

       // set the from information 
       InternetAddress from = new InternetAddress(fromEmail, fromName); 
       // set the to information 
       InternetAddress to = new InternetAddress(host_email_address, toName);     

       // Create the message 
        Message msg = new MimeMessage(ssn); 
        msg.setFrom(from); 
        msg.addRecipient(Message.RecipientType.TO, to); 
        msg.addHeader("X-Priority", "1"); 
        msg.setSubject(Subject); 
        msg.setContent(HtmlMessage, "text/html"); 

       String protocol = "smtp"; 

       t = ssn.getTransport(protocol); 
        t.connect(SMTP,username,password); 
       t.sendMessage(msg, msg.getAllRecipients());     

       res.sendRedirect(res.encodeRedirectURL("MyAccount.jsp?message=Email%20successfully%20sent.")); 
       return;                  

       }//try 
       catch (MessagingException mex) {    
         mex.printStackTrace(); 
         } 
       catch(Exception e) {}     
        finally { 
         try { 
          t.close(); 
          } 
          catch(Exception e) {} 
         }//finally 

回答

2

我假設你有一個代碼塊,像這樣的地方:

Transport transport = session.getTransport("smtp"); 
transport.connect(); 
transport.sendMessage(message, message.getAllRecipients()); 
transport.close(); 

你調用關閉方法?

另外,調用這個的頻率是多少?它可能只是您的提供商練習垃圾郵件預防。

+0

我在Transport對象上調用close(),我剛剛檢查了我的代碼。實際上,我將發佈我在其中一個servlet中用於發送電子郵件的方法的片段。也許你會發現我沒有看到的東西。我將追加代碼到我原來的問題...... – katura 2010-12-15 16:32:01

+0

我也想知道,如果提供者限制我。對於我在我的網站上測試的這個特定流程,將發送2封電子郵件。 1給我自己,1給客戶。我第一次嘗試它,它是成功的。這兩封電子郵件都已發送。第二次,只有第一次,然後它錯了。 7分鐘後,我通過不同的jsp/servlet頁面發送了一封電子郵件,並且電子郵件通過了。 – katura 2010-12-15 16:48:16

+0

我認爲問題已經解決。我的servlet中有一個方法發送2封電子郵件。與此有關的問題是,我一個接一個地與運輸公司建立了2個連接,因爲我有2封電子郵件要發送。傳輸對象需要在每次連接請求後關閉。我做了一些測試,並且電子郵件正在成功發送。所以現在問題已經解決:)謝謝你的幫助。 – katura 2010-12-15 18:47:55

0
/*SIMPLE EMAIL TO GMAIL OR YAHOO MAIL USING JAVA*/ 
import java.awt.HeadlessException; 
import java.util.Properties; 
import javax.mail.Address; 
import javax.mail.Message; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class JavaMail { 

    public static void main(String args[]) { 
     new JavaMail().email(); 
    } 

    public void email() { 
     String from = "[email protected]"; 
     String password = "your_secret_password"; 
     String to = "[email protected]"; 
     String subject = "email_subject"; 
     String msg = "email_message"; 
     Properties properties = System.getProperties(); 
     properties = setProp(from, to); 
     Session session = Session.getDefaultInstance(properties); 

     try { 
      Address address = new InternetAddress(to, "LovenishGoyal"); 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(address); 
      message.addRecipient(Message.RecipientType.TO, address); 
      message.setSubject(subject); 
      message.setText(msg); 
      message.saveChanges(); 
      Transport transport = session.getTransport(); 
      System.out.println("connecting..."); 
      transport.connect(from, password); 
      System.out.println("connected!"); 
      System.out.println("sending..."); 
      transport.sendMessage(message, message.getAllRecipients()); 
      transport.close(); 
      System.out.println("Sent message successfully...."); 

     } catch (Exception mex) { 
      mex.printStackTrace(); 
     } 

    } 

    private Properties setProp(String email, String targetEmail) { 
     Properties props = null; 
     try { 
      props = System.getProperties(); 
      if (email.contains(",") || targetEmail.contains(",")) { 
       System.out.println("Please send one email to one person at a time..."); 
      } else if (email.contains("@yahoo.com")) { 
       props.put("mail.smtp.host", "smtp.mail.yahoo.com"); 
       props.put("mail.smtp.socketFactory.port", "465"); 
       props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
       props.put("mail.smtp.host", "smtp.mail.yahoo.com"); 
       props.put("mail.smtp.auth", true); 
      } else if (email.contains("@gmail.com")) { 
       props.put("mail.smtp.host", "smtp.gmail.com"); 
       props.put("mail.smtp.socketFactory.port", "465"); 
       props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
       props.put("mail.smtp.auth", "true"); 
       props.put("mail.smtp.port", "465"); 
      } else { 
       System.out.println("Your Email Address is invalid\n or host not supported!"); 
      } 

     } catch (HeadlessException exp) { 
      System.out.println(exp); 
     } 
     return props; 
    } 

} 
相關問題