5

所以,我正在尝试通过 javamail 和 glassfish 发送电子邮件:这是我的代码(我在这里找到它http://www.javasrilankansupport.com/2012/05/send-email-in-java-mail-api -使用-gmail.html

private Session m_Session;
private Message m_simpleMessage;
private InternetAddress m_fromAddress;
private InternetAddress m_toAddress;
private Properties m_properties;
@Override
public void sendMail(String mail) throws Exception {
    try {

        m_properties     = new Properties();
        m_properties.put("mail.smtp.host", "smtp.gmail.com");
        m_properties.put("mail.smtp.socketFactory.port", "465");
        m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        m_properties.put("mail.smtp.auth", "true");
        m_properties.put("mail.smtp.port", "465");
        m_properties.put("mail.debug", "false");
        m_properties.put("mail.smtp.ssl.enable", "true");


        m_Session        =   Session.getDefaultInstance(m_properties,new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("user@gmail.com","password"); // username and the password
            }

        });

        m_simpleMessage  =   new MimeMessage(m_Session);

        m_fromAddress    =   new InternetAddress("user@gmail.com");
        m_toAddress      =   new InternetAddress(mail);


        m_simpleMessage.setFrom(m_fromAddress);
        m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
        m_simpleMessage.setSubject("Test letter");
        m_simpleMessage.setContent("Hi, this is test letter.","text/plain");

        Transport.send(m_simpleMessage);

    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
}

但是在服务器上使用此功能后,我发现了异常:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at com.kma.summer2012.facade.PasswordRecoveryFacadeImpl.sendMail(PasswordRecoveryFacadeImpl.java:49)
    at com.kma.summer2012.server.CrumbsUserServiceImpl.sendMailRecovery(CrumbsUserServiceImpl.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    ... 42 more

据我了解,该功能想要使用 localhost 和 25 端口而不是 gmail 的主要问题,我不知道为什么。注意 telnet smtp.gmail.com 25,telnet smtp.gmail.com 465 工作良好。任何建议这是什么原因?

4

1 回答 1

2

尝试修复其中一些常见的 JavaMail 错误。另请参阅如何使用 JavaMail 连接到 Gmail

于 2013-02-06T21:45:35.700 回答