-3

電子メールを送信できる 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>

私が見ているエラーは次のとおりです。

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 37 in the jsp file: /pizza/page/ssl.jsp
The constructor MimeMessage(HttpSession) is undefined
4

2 に答える 2

7

エラー メッセージが示すとおり、. を受け取るコンストラクタはありませんMimeMessageHttpSession受け取るコンストラクターがあります。これはjavax.mail.Session、使用しようとしているもののように見えますが、HttpSessionは ではありませんjavax.mail.Session

Session.getDefaultInstance()現在破棄しているによって返された値を渡します。

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

// ...

Message message = new MimeMessage(mailSession);
于 2012-08-17T12:48:47.990 に答える
1
Message message = new MimeMessage(session ); // error

JSP ページ セッションでは、実際には javax.servlet.http.HttpSession である暗黙的なオブジェクトです。

HttpSession とメール javax.mail.Session.Session を混同していると思います。コードを次のように変更します - ...

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

...

Message message = new MimeMessage(mailSession );

...

于 2012-08-17T12:57:01.060 に答える