【问题标题】:JSP email sending errorJSP 邮件发送错误
【发布时间】:2014-09-04 13:07:52
【问题描述】:

我正在创建一个 Web 应用程序来向某个收件人发送电子邮件。当我运行这段代码时,我遇到了这样的异常

org.apache.jasper.JasperException: 在第 27 行处理 JSP 页面 /two.jsp 时发生异常

24:        props.put("mail.smtp.auth", "true");
25:        props.put("mail.smtp.port", "465");
26: 
27:        Session mailSession = Session.getDefaultInstance(props,
28:                new javax.mail.Authenticator() {
29:                      protected PasswordAuthentication  
30:                         getPasswordAuthentication() {

我该怎么办?这是我完整的jsp代码。

            <html>
            <body>

            <%@ page import="java.util.Properties" %>               
            <%@ page import="javax.mail.Message" %>
            <%@ page import="javax.mail.MessagingException" %>
            <%@ page import="javax.mail.PasswordAuthentication" %>
            <%@ page import="javax.mail.Session" %>
            <%@ page import="javax.mail.Transport" %>
            <%@ page import="javax.mail.internet.InternetAddress" %>
            <%@ page import="javax.mail.internet.*" %>


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

                    Session mailSession = Session.getDefaultInstance(props,
                            new javax.mail.Authenticator() {
                                protected PasswordAuthentication 
                                      getPasswordAuthentication() {
                                    return new PasswordAuthentication
                                      ("senderUsername","senderPassword");
                                }
                            });

                    try {

                        Message message = new MimeMessage(mailSession );
                        message.setFrom(new InternetAddress("senderemail@gmail.com"));
                        message.setRecipients(Message.RecipientType.TO,
                                InternetAddress.parse("recipient@gmail.com"));
                        message.setSubject("hi");
                        message.setText("text contrnt" +
                                "\n\n Test email");

                        Transport.send(message);

                        System.out.println("Done");

                    } catch (MessagingException e) {
                        throw new RuntimeException(e);
                    }
            %>
            </body>
            </html>

有什么帮助吗? 谢谢。

【问题讨论】:

    标签: java jsp email smtp jakarta-mail


    【解决方案1】:

    Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication ("",""); } });

    使用 Session.getInstance() 而不是 Session.getDefaultInstance()。

    【讨论】:

    • 我试过这个。没有错误。代码正在编译和运行。但未发送电子邮件。什么错误?
    • 我没有收到上述代码的任何错误,我正在接收电子邮件。
    • 当我运行此代码页时,将在浏览器中打开并加载它,而无需发送电子邮件。那该怎么办?
    • 你能看到控制台打印的“Done”字符串吗?
    • return new PasswordAuthentication("myUsername","myPassword"); message.setFrom(new InternetAddress("myEmailAddress@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@gmail.com"));我设置了这个地址。我只提供了一个例子。在我的程序中,我使用了我的原始地址
    【解决方案2】:

    转到https://www.google.com/settings/security 并禁用两步验证并试一试。

    【讨论】:

    • 我试过这种方式。同样的事情也在发生。我无法相信这是什么错误。我已经尝试了很多教程。但同样的事情发生了。如何解决这个问题。这是我的应用程序中的无聊问题。 :(
    【解决方案3】:

    首先,修复这些common mistakes

    接下来,使用这些debugging tips 并发布调试输出,如果您仍然无法弄清楚。

    您可能希望用 try/catch 包围您的代码,以确保您不会错过抛出的异常。

    您没有说明您使用的是什么应用程序服务器,但如果它不是 Java EE 应用程序服务器,请确保 JavaMail jar 文件位于服务器的“lib”目录或 Web 应用程序的“lib”目录中。

    【讨论】:

      猜你喜欢
      • 2013-09-06
      • 2017-10-22
      • 2010-10-18
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2013-01-07
      相关资源
      最近更新 更多