Reputation: 21
i'm doing a school project and i could use some help with this code: i'm trying to invoke this method on a java page:
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("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
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);
}
}
}
I'm calling this function on a jsp page:
<%@ 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>
the java class is imported currectly but the eclipse refuses to run this program. i must add that i'm using eclipse galileo with apache tomcat. am i doing something wrong?
Upvotes: 1
Views: 316
Reputation: 8386
You appear to be trying to call a method called test()
. Your class has a method called send()
instead.
Also, since it's a static method, you don't need an instance of the class to call it. Simply call SendMail.send();
.
Upvotes: 1
Reputation: 114
after you compile the code successfully, place it under the web-inf/classes/ folder. place all the jar file you need to compile this class under the web-inf/lib folder.
within the jsp page you should have this
......
< %@ page import="test.SendMail" % >
<% //call the function here
SendMail.send();
%>
.....
Upvotes: 1
Reputation: 1503839
I suspect this is the problem:
charset=windows-1255
I suggest you use UTF-8 instead. It would be far more portable.
(There's no obvious sign that you even are calling the method from the page.)
By the way, currently you've got a method with the same name as your class - I would strongly discourage you from doing that. Aside from anything else, Java conventions are to use camelCase
for method names.
Upvotes: 2