java - Using JavaMail with a Self Signed Certificate -
i have servlet contains following code:
system.setproperty("javax.net.ssl.keystore", getservletcontext().getrealpath("keystore.jks")); system.setproperty("javax.net.ssl.keystorepassword", "123456"); system.setproperty("javax.net.ssl.truststore",getservletcontext().getrealpath("keystore.jks")); system.setproperty("javax.net.ssl.truststorepassword","123456"); sslsocketfactory sslsocketfactory = (sslsocketfactory) sslsocketfactory.getdefault(); sslsocket sslsocket = (sslsocket) sslsocketfactory.createsocket("127.0.0.1", 606);
with code i'm able use ssl (what mandatory case). later in same servlet need send e-mail using javamail. when try send e-mail without using certificate in other code, works fine, when try using following message returned: using port 465 ->
debug: setdebug: javamail version 1.4.7 debug: setdebug: javamail version 1.4.7 debug: getprovider() returning javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,oracle] debug smtp: useehlo true, useauth true debug smtp: useehlo true, useauth true debug smtp: trying connect host "smtp.gmail.com", port 465, isssl false
aaand stuck on =p.
using port 587 ->
debug: setdebug: javamail version 1.4.7 debug: getprovider() returning javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,oracle] debug smtp: useehlo true, useauth true debug smtp: useehlo true, useauth true debug smtp: trying connect host "smtp.gmail.com", port 587, isssl false 220 mx.google.com esmtp g66sm55412571yhd.11 - gsmtp debug smtp: connected host "smtp.gmail.com", port: 587 ehlo rai-pc 250-mx.google.com @ service, [201.82.118.174] 250-size 35882577 250-8bitmime 250-starttls 250 enhancedstatuscodes debug smtp: found extension "size", arg "35882577" debug smtp: found extension "8bitmime", arg "" debug smtp: found extension "starttls", arg "" debug smtp: found extension "enhancedstatuscodes", arg "" debug smtp: use8bit false mail from:<raiavancinifranco@gmail.com> 530 5.7.0 must issue starttls command first. g66sm55412571yhd.11 - gsmtp debug smtp: got response code 530, response: 530 5.7.0 must issue starttls command first. g66sm55412571yhd.11 - gsmtp rset 250 2.1.5 flushed g66sm55412571yhd.11 - gsmtp debug smtp: messagingexception while sending, throw: com.sun.mail.smtp.smtpsendfailedexception: 530 5.7.0 must issue starttls command first. g66sm55412571yhd.11 - gsmtp @ com.sun.mail.smtp.smtptransport.issuesendcommand(smtptransport.java:2108) @ com.sun.mail.smtp.smtptransport.mailfrom(smtptransport.java:1609) @ com.sun.mail.smtp.smtptransport.sendmessage(smtptransport.java:1117) @ javax.mail.transport.send0(transport.java:195) @ javax.mail.transport.send(transport.java:124) @ servlet.main.service(main.java:221) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:472) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:99) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:953) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1023) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:589) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:310) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source) quit 221 2.0.0 closing connection g66sm55412571yhd.11 - gsmtp jul 10, 2013 7:04:40 pm org.apache.catalina.core.standardwrappervalve invoke severe: servlet.service() servlet [servletmain] in context path [/servicotcc] threw exception java.lang.runtimeexception: com.sun.mail.smtp.smtpsendfailedexception: 530 5.7.0 must issue starttls command first. g66sm55412571yhd.11 - gsmtp @ servlet.main.service(main.java:226) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:472) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:99) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:953) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1023) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:589) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:310) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source) caused by: com.sun.mail.smtp.smtpsendfailedexception: 530 5.7.0 must issue starttls command first. g66sm55412571yhd.11 - gsmtp @ com.sun.mail.smtp.smtptransport.issuesendcommand(smtptransport.java:2108) @ com.sun.mail.smtp.smtptransport.mailfrom(smtptransport.java:1609) @ com.sun.mail.smtp.smtptransport.sendmessage(smtptransport.java:1117) @ javax.mail.transport.send0(transport.java:195) @ javax.mail.transport.send(transport.java:124) @ servlet.main.service(main.java:221) ... 17 more
the properties code:
properties props = new properties(); 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");
i can't send e-mail (i'm not sure cause). should do?
-- edit --
after importing google smtp certificate keystore , changing port , properties to:
properties props = new properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketfactory.port", "465"); props.put("mail.smtp.socketfactory.class", sslsocketfactory); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.starttls.enable", "true");
my output changed to:
debug: setdebug: javamail version 1.4.7 debug: getprovider() returning javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,oracle] debug smtp: useehlo true, useauth true debug smtp: useehlo true, useauth true debug smtp: trying connect host "smtp.gmail.com", port 465, isssl true jul 10, 2013 8:26:46 pm org.apache.catalina.core.standardwrappervalve invoke severe: servlet.service() servlet [servletmain] in context path [/servicotcc] threw exception java.lang.runtimeexception: javax.mail.messagingexception: not connect smtp host: smtp.gmail.com, port: 465; nested exception is: javax.net.ssl.sslhandshakeexception: sun.security.validator.validatorexception: pkix path building failed: sun.security.provider.certpath.suncertpathbuilderexception: unable find valid certification path requested target @ servlet.main.service(main.java:229) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:472) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:99) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:953) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1023) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:589) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:310) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source) caused by: javax.mail.messagingexception: not connect smtp host: smtp.gmail.com, port: 465; nested exception is: javax.net.ssl.sslhandshakeexception: sun.security.validator.validatorexception: pkix path building failed: sun.security.provider.certpath.suncertpathbuilderexception: unable find valid certification path requested target @ com.sun.mail.smtp.smtptransport.openserver(smtptransport.java:1961) @ com.sun.mail.smtp.smtptransport.protocolconnect(smtptransport.java:654) @ javax.mail.service.connect(service.java:317) @ javax.mail.service.connect(service.java:176) @ javax.mail.service.connect(service.java:125) @ javax.mail.transport.send0(transport.java:194) @ javax.mail.transport.send(transport.java:124) @ servlet.main.service(main.java:224) ... 17 more caused by: javax.net.ssl.sslhandshakeexception: sun.security.validator.validatorexception: pkix path building failed: sun.security.provider.certpath.suncertpathbuilderexception: unable find valid certification path requested target @ sun.security.ssl.alerts.getsslexception(unknown source) @ sun.security.ssl.sslsocketimpl.fatal(unknown source) @ sun.security.ssl.handshaker.fatalse(unknown source) @ sun.security.ssl.handshaker.fatalse(unknown source) @ sun.security.ssl.clienthandshaker.servercertificate(unknown source) @ sun.security.ssl.clienthandshaker.processmessage(unknown source) @ sun.security.ssl.handshaker.processloop(unknown source) @ sun.security.ssl.handshaker.process_record(unknown source) @ sun.security.ssl.sslsocketimpl.readrecord(unknown source) @ sun.security.ssl.sslsocketimpl.performinitialhandshake(unknown source) @ sun.security.ssl.sslsocketimpl.starthandshake(unknown source) @ sun.security.ssl.sslsocketimpl.starthandshake(unknown source) @ com.sun.mail.util.socketfetcher.configuresslsocket(socketfetcher.java:549) @ com.sun.mail.util.socketfetcher.createsocket(socketfetcher.java:354) @ com.sun.mail.util.socketfetcher.getsocket(socketfetcher.java:237) @ com.sun.mail.smtp.smtptransport.openserver(smtptransport.java:1927) ... 24 more caused by: sun.security.validator.validatorexception: pkix path building failed: sun.security.provider.certpath.suncertpathbuilderexception: unable find valid certification path requested target @ sun.security.validator.pkixvalidator.dobuild(unknown source) @ sun.security.validator.pkixvalidator.enginevalidate(unknown source) @ sun.security.validator.validator.validate(unknown source) @ sun.security.ssl.x509trustmanagerimpl.validate(unknown source) @ sun.security.ssl.x509trustmanagerimpl.checktrusted(unknown source) @ sun.security.ssl.x509trustmanagerimpl.checkservertrusted(unknown source) ... 36 more caused by: sun.security.provider.certpath.suncertpathbuilderexception: unable find valid certification path requested target @ sun.security.provider.certpath.suncertpathbuilder.enginebuild(unknown source) @ java.security.cert.certpathbuilder.build(unknown source) ... 42 more
get rid of socket factory stuff:
setting various socketfactory properties. long, long ago javamail didn't have built in support ssl connections, necessary set these properties use ssl. hasn't been case years; remove these properties , simplify code. easiest way enable ssl support in current versions of javamail set property "mail.smtp.ssl.enable" "true". (replace "smtp" "imap" or "pop3" appropriate.)
see these gmail examples.
the gmail certificate needs in trust store, not key store.
Comments
Post a Comment