Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过jsp发送电子邮件_Java_Jsp_Email - Fatal编程技术网

Java 通过jsp发送电子邮件

Java 通过jsp发送电子邮件,java,jsp,email,Java,Jsp,Email,我试图设计一个jsp页面,在这里我可以发送电子邮件,但是在核心java中正确运行的代码在jsp代码中使用时给了我一个异常。我相信我已经将mail.jar正确地放在lib中了 当前jsp代码为: <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <!DOCTYPE html PUBLIC

我试图设计一个jsp页面,在这里我可以发送电子邮件,但是在核心java中正确运行的代码在jsp代码中使用时给了我一个异常。我相信我已经将mail.jar正确地放在lib中了

当前jsp代码为:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <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.MimeMessage" %>


                <%
                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.getDefaultInstance(props,
                            new javax.mail.Authenticator() {
                                protected PasswordAuthentication getPasswordAuthentication() {
                                    return new PasswordAuthentication("prakash.d2222","**************");
                                }
                            });

                        try {

                            Message message = new MimeMessage(session);
                            message.setFrom(new InternetAddress("from@no-spam.com"));
                            message.setRecipients(Message.RecipientType.TO,
                                    InternetAddress.parse("prakash_d22@rediffmail.com"));
                            message.setSubject("hi");
                            message.setText("12345" +
                                    "\n\n No spam to my email, please!");

                            Transport.send(message);

                            System.out.println("Done");

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

正如错误消息告诉您的那样。有一个构造函数,它接受一个,它似乎就是您试图使用的构造函数,但它不是一个
javax.mail.Session

传递当前正在丢弃的会话.getDefaultInstance()返回的值

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

// ...

Message message = new MimeMessage(mailSession);
JSP页面会话中有一个隐式对象,实际上是javax.servlet.http.HttpSession

我认为您对HttpSession和mail javax.mail.Session.Session感到困惑。改变你 代码式-


请传递一个
javax.mail.Session
而不是一个
HttpSession
@prakash\u d22,展示一些解决您自己问题的努力。所以不是一个给我看codez的网站。请仔细阅读。@MattBall,+1表示容差:)@MattBall它工作过一次,但在会话声明中显示错误line@prakash_d22错误是。。。?来吧,伙计。你自己有没有努力解决这个问题?-1感谢评论中粗鲁无礼的“给我密码”行为。在一个无关的注释中,请学会区分关注点。这个特定的问题实际上与JSP无关。您应该首先创建一个独立的类来完成所有的JavaMail任务,并使用带有
main()
方法的普通Java应用程序来测试它。一旦您让它工作起来,您所需要做的就是在JSP中调用新创建的类/方法(或者,更好地说,).i do it man,它在core java中正常运行,但仅jsp中出现问题。Anks everyone for helpnow会话显示异常。使用了try and catch,但未发送电子邮件。但以前的错误已通过thnks解决。您能否提供以下代码的堆栈跟踪try{session mailSession=session.getDefaultInstance(props,new javax.mail.Authenticator(){protected PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication(“prakash.d222”、“****************”;}}});Message Message=new mimessage(mailSession);@prakash_d22-请在此处使用完整的电子邮件id尝试-“new PasswordAuthentication”("me@domain.com“,”secrect“;”上述代码没有问题,两个代码都工作正常再次感谢,如果需要进一步帮助,将再次在我的jsp网站上寻求帮助
Session mailSession = Session.getDefaultInstance(props,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("prakash.d2222","**************");
        }
});

// ...

Message message = new MimeMessage(mailSession);
Message message = new MimeMessage(session ); // error
 Session mailSession = Session.getDefaultInstance(props,
                                new javax.mail.Authenticator() {
                                    protected PasswordAuthentication 
                                          getPasswordAuthentication() {
                                        return new PasswordAuthentication
                                          ("prakash.d2222","**************");
                                    }
                                });
Message message = new MimeMessage(mailSession );