【问题标题】:Session cannot be resolved to a type for sending mail via jsp会话无法解析为通过 jsp 发送邮件的类型
【发布时间】:2015-06-16 15:11:58
【问题描述】:

我想从我的 jsp 页面发送一封电子邮件。我正在使用 Apache Tomcat 服务器并在 localhost 上运行。我参考了这个链接http://www.javabeat.net/send-mail-jsp/ 来指导我。我还在 JangoSMTP 上创建了一个帐户,并在下面的代码中提供了用户名和密码。然而,我得到的错误是:

An error occurred at line: 24 in the jsp file: /index2.jsp
Session cannot be resolved to a type
21:         props.put("mail.smtp.port", "25");
22:      
23:         //Get the Session object.
24:         Session mailSession = Session.getInstance(props,
25:                 new javax.mail.Authenticator() {
26:                     protected PasswordAuthentication getPasswordAuthentication() {
27:                         return new PasswordAuthentication(username,

这是我的 index.html:-

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
        <%@ page import="javax.mail.internet.*,javax.activation.*"%>
        <%@ page import="javax.servlet.http.*,javax.servlet.*"%>
        <%
            String result;
            //Recipient's email ID needs to be mentioned.
            String to = "xyz@gmail.com";

            // Sender's email ID needs to be mentioned
            String from = "abc@gmail.com";
            final String username = "username";
            final String password = "password";

            // Assuming you are sending email through relay.jangosmtp.net
            String host = "relay.jangosmtp.net";

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "25");

            //Get the Session object.
            Session mailSession = Session.getInstance(props,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username,
                                    password);
                        }
                    });

            try {
                // Create a default MimeMessage object.
                Message message = new MimeMessage(mailSession);

                // Set From: header field of the header.
                message.setFrom(new InternetAddress(from));

                // Set To: header field of the header.
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(to));

                // Set Subject: header field
                message.setSubject("Testing Subject");

                // Now set the actual message
                message.setText("Hello, this is sample for to check send "
                        + "email using JavaMailAPI in JSP ");

                // Send message
                Transport.send(message);

                System.out.println("Sent message successfully....");
                result = "Sent message successfully....";

            } catch (MessagingException e) {
                e.printStackTrace();
                result = "Error: unable to send message....";

            }
        %>
        <html>
        <head>
        <title>Send Email using JSP</title>
        </head>
        <body>
            <center>
                <h1>Send Email using JSP</h1>
            </center>
            <p align="center">
                <%
                    out.println("Result: " + result + "\n");
                %>
            </p>
        </body>
        </html>

请告诉我错误在哪里!我google了很多,但找不到答案。提前致谢!

【问题讨论】:

  • 您在 CLASSPATH 或 lib 文件夹中是否有 mail.jar
  • @Rembo:在 CLASSPATH 中。会有什么不同吗?
  • 好吧..我认为这是错误的。但是现在我收到错误:结果:错误:无法发送消息....那么,我应该如何解决呢?
  • 我在控制台中收到错误消息:javax.mail.SendFailedException: Sending failed;嵌套异常是:类 javax.mail.MessagingException:未知 SMTP 主机:relay.jangosmtp.net;
  • 用您当前的问题详细信息更新您的问题并完成堆栈跟踪。

标签: jsp email session tomcat smtp


【解决方案1】:

您包含 .jar 文件。

$TOMCAT_HOME 垃圾桶 会议 库 ... 网络应用 test.jsp(可以在WEB-INF以外的任何目录下) 网络信息 库 邮件.jar 激活.jar

【讨论】:

  • 能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?
猜你喜欢
  • 2011-10-08
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
相关资源
最近更新 更多