jsp - session 无法解析为通过 jsp 发送邮件的类型

标签 jsp email session tomcat smtp

我想从我的 jsp 页面发送电子邮件。我正在使用 Apache Tomcat 服务器并在本地主机上运行。我提到了这个链接 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>

请告诉我错误在哪里!我用谷歌搜索了很多但找不到答案。提前致谢!

最佳答案

您包含 .jar 文件。

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

关于jsp - session 无法解析为通过 jsp 发送邮件的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23253848/

相关文章:

php imap 从电子邮件地址获取

php - 无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件

session - 使用 wsadmin 列出事件 session

php - 安全的 PHP 登录系统?

java - 没有错误,没有输出,数据没有插入到jsp的数据库中

java - 如何获取 JSP 页面之外的 Web 应用程序路径?

java - base64 到文件响应不起作用

c# - 如何创建一个 csv 并附加到电子邮件并在 C# 中发送

javascript - 使用 Passportjs 刷新页面后保持身份验证

javascript - 如何在 javascript 中嵌入 JSTL forEach