Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Default Javamail Session in Websphere with GMail

I'm trying to create an javamail Session in Websphere 7 to use GMail, but my authentication always fail.

Yes man, my password is ok ...

I'm configuring my Session as:

Output Mail Properties

  • Provider: Built-in Mail Provider
  • JNDI: mail/session/Default
  • Debug: On
  • Server: smtp.gmail.com
  • Protocol: smtp
  • User: [email protected]
  • Password: mypassword
  • Confirm Password: same as above
  • Return email: [email protected]

In the Session Properties, I have:

  • mail.smtp.auth = true
  • mail.smtp.port = 465
  • mail.smtp.starttls.enable = true
  • mail.smtp.socketFactory.port = 465
  • mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory

I'm using an test JSP with something like ...

...
System.out.println("Entering MailJsp!");

String errorMessage = null;
String destinationAddress = request.getParameter("destinationAddress");
String messageBody = request.getParameter("message");
String subject = "Mail from MailServlet";
String emailFrom = "[email protected]"; 

// try only if it is not the first request
if (destinationAddress != null) {

    System.out.println("Sending email");
    try 
    {
        // look up MailSession
        Context context = new InitialContext();
        Session mailSession =  (Session)context.lookup("mail/session/Default");

        System.out.println("Mail Session Properties: " + mailSession.getProperties());
        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(emailFrom));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinationAddress));

        // Set the subject and body text
        msg.setSubject(subject);
        msg.setText(messageBody);

        // send message
        Transport.send(msg);
        System.out.println("Email Sent");
    }
    catch (Exception e) 
    {
        System.out.println("Error sending Email: " + e);
        e.printStackTrace();
        errorMessage = e.getMessage() != null ? e.getMessage() : e.toString();
    }
}
System.out.println("Exiting MailJsp");

...

... and, my result log is:

Entering MailJsp!
Sending email
DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: C:\environment\server\IBM\WAS7\AppServer\java\jre\lib\javamail.providers (The system cannot find the file specified.)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\environment\server\IBM\WAS7\AppServer\java\jre\lib\javamail.address.map (The system cannot find the file specified.)
*** In SessionFactory.setPasswordAuthentication, TRANSPORT PasswordAuthentication is based on:
    url=smtp://[email protected]@smtp.gmail.com:465
    [email protected]
    password=********
*** In SessionFactory.getObjectInstance, session properties:
    mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
    mail.transport.protocol=smtp
    mail.imaps.class=com.sun.mail.imap.IMAPSSLStore
    mail.smtp.host=smtp.gmail.com
    mail.debug=true
    [email protected]
    mail.pop3s.class=com.sun.mail.pop3.POP3SSLStore
    mail.smtp.starttls.enable=true
    mail.smtp.port=465
    mail.smtp.class=com.sun.mail.smtp.SMTPTransport
    mail.smtp.auth=true
    mail.smtps.class=com.sun.mail.smtp.SMTPSSLTransport
    mail.smtp.socketFactory.port=465
    [email protected]
    mail.imap.class=com.sun.mail.imap.IMAPStore
    mail.mime.address.strict=true
    mail.pop3.class=com.sun.mail.pop3.POP3Store
Mail Session Properties: {mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory, mail.transport.protocol=smtp, mail.imaps.class=com.sun.mail.imap.IMAPSSLStore, mail.smtp.host=smtp.gmail.com, mail.debug=true, [email protected], mail.pop3s.class=com.sun.mail.pop3.POP3SSLStore, mail.smtp.starttls.enable=true, mail.smtp.port=465, mail.smtp.class=com.sun.mail.smtp.SMTPTransport, mail.smtp.auth=true, mail.smtps.class=com.sun.mail.smtp.SMTPSSLTransport, mail.smtp.socketFactory.port=465, [email protected], mail.imap.class=com.sun.mail.imap.IMAPStore, mail.mime.address.strict=true, mail.pop3.class=com.sun.mail.pop3.POP3Store}
DEBUG: mail.smtp.class property exists and points to com.sun.mail.smtp.SMTPTransport
DEBUG SMTP: useEhlo true, useAuth true
Error sending Email: javax.mail.AuthenticationFailedException
javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:306)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:105)
    at javax.mail.Transport.send0(Transport.java:168)
    at javax.mail.Transport.send(Transport.java:98)
    at com.ibm._jsp._email._jspService(_email.java:127)
    at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:98)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1443)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1384)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:104)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:852)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:785)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:443)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:175)
    at com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest(GenericServletWrapper.java:121)
    at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:234)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3610)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:274)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:926)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1557)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:173)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:455)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:384)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:272)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:202)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:766)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:896)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1527)
Exiting MailJsp

If I use in another way, it works!

Session mailSession =  (Session)context.lookup("mail/session/Default");
Properties props = mailSession.getProperties();

mailSession = Session.getDefaultInstance(
    props,
    new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("[email protected]","mypassword");
        }
    });

But, I want to use the password set in the Administrative Console, and not provide programatically. So the user can manage their passwords.

Can someone help please ?

tks

like image 859
Sebastião Santos Avatar asked Jun 11 '13 23:06

Sebastião Santos


1 Answers

You can fix the problem by adding singer certificate for gmail smtp:

  1. Log in to Admin console of WebSphere
  2. Security -> Security Certificate and Key Management
  3. Select Key Store and Certificates
  4. Select Node Default Key Store,then you will reach another page please visit next page to continue to fix your issue about Email Sending.
  5. On NodeDefaultKeyStore page, Select "Signer Certifiicate" (right-hand side)
  6. Press "Retrieve from port" button
  7. Fill in the form:
    • host: smtp.gmail.com
    • port: 465 (remember we are doing for ssl)
    • alias: gmail.com
  8. Press OK than will see "save" - so click on it as well After SSL certificate and key management > Key stores and certificates you will be able to see ssl cert for java. Please restart you app server after your jsp should work.

Problem is that Websphere does not trust to gmail ssl cert, so that's an issue. I managed to fix same problem by adding the cert hope you will benefit from my instructyion

like image 103
Gabit Kemelov Avatar answered Nov 09 '22 05:11

Gabit Kemelov