使用JavaMail发送电子邮件,由于“连接太多”而导致“连接被拒绝” - 为什么?

时间:2010-12-15 15:48:31

标签: java email smtp javamail connection

我的网站基于JSP页面和servlet。我在我的vps上运行Tomcat 6.

我正在使用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)

我通过 MY 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 个答案:

答案 0 :(得分:2)

我假设您在某处有一段代码:

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

你在调用close方法吗?

此外,拨打此频率的频率是多少?它可能只是您的提供商实施垃圾邮件预防。

答案 1 :(得分: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 = "your_email@domain.com";
        String password = "your_secret_password";
        String to = "email_to@domain.com";
        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;
    }

}