使用JavaMail发送电子邮件,收到“连接被拒绝”因为“连接太多” - 为什么?

发布于 2024-10-08 02:48:29 字数 7127 浏览 6 评论 0原文

我的站点基于 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)

我通过我的 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

My site is based on JSP pages and servlets. I run Tomcat 6 on my vps.

I'm using JavaMail version 1.4 to send emails on my site.

I can send off a few emails during my session, but today I got a blank page on the submission page of my site. So I looked at the Tomcat logs and here's what happened -

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)

I send my emails through MY ISP provider. The one I use for internet at home. This is not the same provider that my VPS runs on.

So is this problem a result of my ISP provider and whatever is set on their end, or does this have to do with my code in my servlet?

The log said "too many connections", so initially I'm wondering if this is something on my end, and if there is something I should be closing (in the servlet, call close() method on something?) after the email is sent?

Edited post to include snippet from 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必在意 2024-10-15 02:48:29

我假设您在某处有这样的代码块:

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

您正在调用 close 方法吗?

另外,调用此功能的频率是多少?可能只是您的提供商在预防垃圾邮件。

I assume that you have a block of code like this somewhere:

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

Are you calling the close method?

Also, what is the frequency of calls to this? It may just be your provider practicing spam prevention.

一袭水袖舞倾城 2024-10-15 02:48:29
/*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;
    }

}
/*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;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文