Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send mail using localhost instead of gmail

so, i'm trying to send e-mail via javamail and glassfish: here is my code(i find it here http://www.javasrilankansupport.com/2012/05/send-email-in-java-mail-api-using-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("[email protected]","password"); // username and the password
            }

        });

        m_simpleMessage  =   new MimeMessage(m_Session);

        m_fromAddress    =   new InternetAddress("[email protected]");
        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();
    }
}

but after using this function on server i catch thos exceptions:

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

as i understand, the main problem that function want to use localhost and 25 port instead of gmail, and i can't figure why. Note telnet smtp.gmail.com 25, telnet smtp.gmail.com 465 work well. any suggestions what is the cause of this?

like image 872
Victor Kiricheiko Avatar asked Nov 04 '22 05:11

Victor Kiricheiko


1 Answers

Try fixing some of these common JavaMail mistakes. See also how to connect to Gmail using JavaMail.

like image 198
Bill Shannon Avatar answered Nov 09 '22 17:11

Bill Shannon