我正在做一个学校项目,我可以在这段代码中得到一些帮助: 我正在尝试在 Java 页面上调用此方法:
import java.io.UnsupportedEncodingException;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
package Test;
public class SendMail {
public static void () {
final String username = "";
final String password = "";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("shov.rz@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("shov.rz@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我在 jsp 页面上调用这个函数:
<%@ page language="java" contentType="text/html; charset=windows-1255"
pageEncoding="windows-1255"%>
<%@page import="Test.SendMail"
SendMail.test(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
当前导入了 java 类,但 eclipse 拒绝运行该程序。 我必须补充一点,我正在将 eclipse galileo 与 apache tomcat 一起使用。 我做错了什么吗?
最佳答案
我怀疑这是问题所在:
charset=windows-1255
我建议您改为使用 UTF-8。它会更便携。
(没有明显迹象表明您甚至正在从页面调用该方法。)
顺便说一下,目前您有一个与您的类同名的方法 - 我会强烈阻止您这样做。抛开其他不谈,Java 惯例是使用 camelCase
作为方法名称。
关于java - 在jsp上使用java函数导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16924863/