gpt4 book ai didi

session - 如何在带有 GMail 的 Websphere 中使用默认 Javamail session

转载 作者:行者123 更新时间:2023-12-04 19:09:14 25 4
gpt4 key购买 nike

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

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

我将我的 session 配置为:

Output Mail Properties


  • 提供者:内置邮件提供者
  • JNDI:邮件/ session /默认
  • 调试:开启
  • 服务器:smtp.gmail.com
  • 协议(protocol):smtp
  • 用户:myuser@gmail.com
  • 密码:我的密码
  • 确认密码:同上
  • 回复邮箱:myuser@gmail.com

  • 在 session 属性中,我有:
  • mail.smtp.auth = 真
  • 邮件.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

    最佳答案

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

  • 登录 WebSphere 管理控制台
  • 安全 -> 安全证书和 key 管理
  • 选择 keystore 和证书
  • 选择Node Default Key Store,然后你会到达另一个页面
    请访问下一页以继续解决您关于电子邮件的问题
    发送。
  • 在 NodeDefaultKeyStore 页面上,选择“签名者证书”
    (右侧)
  • 按“从端口检索”按钮
  • 填写表格:
  • 主机:smtp.gmail.com
  • 端口:465(记住我们是为 ssl 做的)
  • 别名:gmail.com
  • 按 OK,然后会看到“保存” - 所以在 SSL 之后也单击它
    证书和 key 管理 > keystore 和您的证书
    将能够看到 java 的 ssl 证书。请重新启动您的应用服务器
    在你的jsp应该工作之后。

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

    关于session - 如何在带有 GMail 的 Websphere 中使用默认 Javamail session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17055578/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com