Help with Javamail
I got this error when i try to send my mail in Index.html
An error occurred at line: -1 in the jsp file: null
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] C:\tomcat41\work\Standalone\localhost\miniproj\Design\Amail\mailer_jsp.java:49: cannot resolve symbol
[javac] symbol : class MailerBean
[javac] location: class org.apache.jsp.mailer_jsp
[javac]MailerBean mailer = null;
[javac]^
[javac] C:\tomcat41\work\Standalone\localhost\miniproj\Design\Amail\mailer_jsp.java:51: cannot resolve symbol
[javac] symbol : class MailerBean
[javac] location: class org.apache.jsp.mailer_jsp
[javac] mailer = (MailerBean) pageContext.getAttribute("mailer", PageContext.PAGE_SCOPE);
[javac]^
[javac] C:\tomcat41\work\Standalone\localhost\miniproj\Design\Amail\mailer_jsp.java:54: cannot resolve symbol
[javac] symbol : class MailerBean
[javac] location: class org.apache.jsp.mailer_jsp
[javac] mailer = (MailerBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "MailerBean");
[javac]^
[javac] 3 errors
Index.html:
<html>
<head>
<style>
div,input,textarea { font-family:Tahoma; font-size:8pt; }
input.std { width:200; }
div.frame { padding-left:70; }
</style>
</head>
<body>
<div class="frame">
<form action="mailer.jsp" method="post">
To :
<input type="text" name="to" class="std"></input>
From :
<input type="text" name="from" class="std"></input>
Subject :
<input type="text" name="subject" class="std"></input>
Message :
<textarea rows="10" cols="80" name="message"></textarea>
<input type="submit" value="Send"></input>
</form>
</div>
</body>
</html>
Mailer.jsp:
<%@ page errorPage="errorPage.jsp" %>
<html>
<head>
<style>
div,input,textarea { font-family:Tahoma; font-size:8pt; }
input.std { width:200; }
div.frame { padding-left:70; color:green; }
</style>
</head>
<body>
<div class="frame">
<jsp:useBean id="mailer" class="com.stardeveloper.bean.test.MailerBean">
<jsp:setProperty name="mailer" property="*"/>
<% mailer.sendMail(); %>
</jsp:useBean>
Email has been sent successfully.
</div>
</body>
</html>
MailerBean.java:
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
public final class MailerBean extends Object implements Serializable {
/* Bean Properties */
private String to = null;
private String from = null;
private String subject = null;
private String message = null;
public static Properties props = null;
public static Session session = null;
static {
/*Setting Properties for STMP host */
props = System.getProperties();
props.put("mail.smtp.host", "mail.yahoo.com");
session = Session.getDefaultInstance(props, null);
}
/* Setter Methods */
public void setTo(String to) {
this.to = to;
}
public void setFrom(String from) {
this.from = from;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setMessage(String message) {
this.message = message;
}
/* Sends Email */
public void sendMail() throws Exception {
if(!this.everythingIsSet())
throw new Exception("Could not send email.");
try {
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
message.setFrom(new InternetAddress(this.from));
message.setSubject(this.subject);
message.setText(this.message);
Transport.send(message);
} catch (MessagingException e) {
throw new Exception(e.getMessage());
}
}
/* Checks whether all properties have been set or not */
private boolean everythingIsSet() {
if((this.to == null) || (this.from == null) ||
(this.subject == null) || (this.message == null))
return false;
if((this.to.indexOf("@") == -1) ||
(this.to.indexOf(".") == -1))
return false;
if((this.from.indexOf("@") == -1) ||
(this.from.indexOf(".") == -1))
return false;
return true;
}
}

