5

我正在尝试在 Websphere 7 中创建一个 javamail 会话以使用 GMail,但我的身份验证总是失败。

是的,伙计,我的密码没问题...

我将我的会话配置为:

输出邮件属性

  • 提供者:内置邮件提供者
  • JNDI:邮件/会话/默认
  • 调试:开
  • 服务器:smtp.gmail.com
  • 协议:smtp
  • 用户:myuser@gmail.com
  • 密码:我的密码
  • 确认密码:同上
  • 回信:myuser@gmail.com

在会话属性中,我有:

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

我正在使用带有类似...的测试JSP

...
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 = "myuser@gmail.com"; 

// 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");

...

...而且,我的结果日志是:

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://myuser@gmail.com@smtp.gmail.com:465
    user=myuser@gmail.com
    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
    mail.from=myuser@gmail.com
    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
    mail.smtp.user=myuser@gmail.com
    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, mail.from=myuser@gmail.com, 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, mail.smtp.user=myuser@gmail.com, 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

如果我以另一种方式使用,它会起作用!

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("myuser@gmail.com","mypassword");
        }
    });

但是,我想使用在管理控制台中设置的密码,而不是以编程方式提供。所以用户可以管理他们的密码。

有人可以帮忙吗?

tks

4

2 回答 2

5

您可以通过为 gmail smtp 添加歌手证书来解决此问题:

  1. 登录 WebSphere 的管理控制台
  2. 安全 -> 安全证书和密钥管理
  3. 选择密钥库和证书
  4. 选择节点默认密钥存储,然后您将进入另一个页面,请访问下一页继续解决您关于电子邮件发送的问题。
  5. 在 NodeDefaultKeyStore 页面上,选择“签名者证书”(右侧)
  6. 按“从端口检索”按钮
  7. 填写表格:
    • 主机:smtp.gmail.com
    • 端口:465(记住我们是为 ssl 做的)
    • 别名:gmail.com
  8. 按确定,然后将看到“保存” - 所以也单击它在 SSL 证书和密钥管理 > 密钥存储和证书之后,您将能够看到 java 的 ssl 证书。请在您的 jsp 正常工作后重新启动您的应用服务器。

问题是 Websphere 不信任 gmail ssl 证书,所以这是一个问题。我设法通过添加证书解决了同样的问题,希望您能从我的指导中受益

于 2015-03-11T11:44:37.313 回答
0

集成解决方案控制台

Mail Sessions > WPexchange > Custom properties > mail.smtp.port

Name: mail.smtp.port
Value: 587
于 2013-10-03T08:47:22.663 回答