我正在创建一个wepage,我希望使用gsp smtp.i发送邮件尝试使用jsp页面,但我没有工作,所以我已经在核心java中成功发送了email.now我想使用这个java我的jsp页面中的代码.i尝试了我但是出现了错误
java代码:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SSL {
public static void main(String [] args){
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 session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("prakash.d2222","password");
}
});
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);
}
}
}
我的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>
<jsp:useBean id="link" scope="application" class = "SSL.class" />
<jsp:setProperty name="link" property="*" />
</body>
</html>
并显示错误
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /pizza/page/ssl.jsp(7,4) The value for the useBean class attribute SSL.class is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1233)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1178)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
org.apache.jasper.compiler.Generator.generate(Generator.java:3459)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:231)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717
所以请帮助我,因为我是jsp的新手。
答案 0 :(得分:3)
考虑以下建议:
.class
个文件必须放在WEB-INF / classes package com.me;
public class SSL {
public void show(){
///
}
}
我不能在您的班级中查看任何getter/setter
方法,因此无需在jsp页面中使用<jsp:setProperty/>
操作,也不包含{{1属性。
class
编辑:
如果您使用IDE(netbeans / eclipse) NOT ,则必须在<jsp:useBean id="link" scope="application" class="com.me.SSL" />
下创建文件夹结构。
/tomcat x.x/webapps/
你必须在JSP页面中调用show()方法:
/webapp <--- This is known as `context` folder
|
|-------/WEB-INF
| |
| |-----------/classes
| | |---/com/me/SSL.class
| |
| |-----------/lib
| mail.jar
| sample.jsp
OR
<jsp:useBean id="link" scope="application" class="com.me.SSL" />
<%
link.show();
%>
答案 1 :(得分:1)
Firslt修改您的课程 SSL
public class SSL {
public void SendMail(){
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 session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("prakash.d2222","password");
}
});
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);
}
}
}
只需创建一个可与您合作的自定义bean类 SSL ,并将其命名为 SSLImpelmenter
你的班级
public class SSLImpelmenter
{
private SSL objSSL = new SSL();
//getter setter methods for objSSL
}
现在在你的JSP中
//Add import for SSL Class
//Now Use useBean tag
<jsp:useBean id="link" scope="application" class = "SSLImpelmenter" />
SSL objSSLJSP = link.getObjSSL();
objSSLJSP.SendMail();
答案 2 :(得分:0)
当前的问题是您在“class = ...”属性中使用文件名而不是类名。由于您的类在默认包中声明(因为您的类中没有package
声明),您应该将useBean
写为:
<jsp:useBean id="link" scope="application" class="SSL" />
如果您还没有这样做,则应确保“SSL.class”文件位于您的webapp的类路径中。例如,它可以位于“/ WEB-INF / classes /”目录中,也可以位于“/ WEB-INF / lib /".
中的JAR文件中。