JSP Email
Hi,
i am trying to send email via a jsp and to start with i found an example that i want to run first as i am only a beginner. however, after pasting the code it underlines the 'bold' words with red....meaning they are not recognised. i am doing this in jbuilder.
I have imported the libraries for javamail.jar and jaf.jar but still no luck.
here's the code:
<%@page import="java.util.*"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.hotmail.com");
Session s =Session.getInstance(props, null);
MimeMessage message = newMimeMessage(s);
InternetAddress from = newInternetAddress(" you@example.comThis email address is being protected from spam bots, you need Javascript enabled to view it ");
message.setFrom(from);
InternetAddress to = newInternetAddress(" you@example.comThis email address is being protected from spam bots, you need Javascript enabled to view it ");
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Test from JavaMail.");
message.setText("Hello from JavaMail!");
Transport.send(message);
.....
The error message i got when trying to compile:
"sendEmail.jsp": package javax.mail does not exist
any ideas?
Thanks
[1486 byte] By [
haggard17a] at [2007-11-27 11:13:43]

>Properties props = new Properties();
use this instead:
Properties props = System.getProperties();
http://www.java2s.com/Code/JavaDownload/SendEmailFromJsp.zip
still no luck just the same error and unrecognised syntaxes.
> The error message i got when trying to compile:
>
> "sendEmail.jsp": package javax.mail does not exist
>
Seems like your mail.jar is not in the classpath.
>I have imported the libraries for javamail.jar and jaf.jar but still no luck.
how ?
add these jars to WEB-INF/lib/
i added it through jbuilder>>required libraries but i think it must still be the problem.
any ideas how to ressolve?
ok that looks to have sorted that. however it obviously doesnt like the following:
InternetAddress from = new InternetAddress(" you@example.comThis email address is being protected from spam bots, you need Javascript enabled to view it ");
do i just hard code an email address in to test it?
what i should put if using two hotmail addresses to test:
props.put("mail.smtp.host", "smtp.mail.hotmail.com");
..as this part is obviously incorrect?
i never used JBuilder, but it is possible that "required libraries" are only used for building your applicaiton, not for runtime. How do you run this?
Did you put these in WEB-INF/lib?
yep i put them in the correct folders and re-added them in the required libraries which works now.
however, the error i am getting after compiling is the following:
Could not connect to SMTP host: mail.hotmail.com, port: 25
any ideas?
can you try
ping mail.hotmail.com on command prompt and see whether or not it is accessible... I dont know whether hotmail provides access to its smtp server to users. But yahoo does.
you're right hotmail doesnt provide that as i read in another article
http://forum.java.sun.com/thread.jspa?threadID=265769&messageID=1014255
however i tried with gmail as suggested but still the same problem i think it might be something to do with authentication and security to stop spamming.
as this program is for demonstration only is there a simpler was i can do this to at least show an email has been sent?
yes, you will need something like this for smtp server authentication.
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
public class SMTPAuthenticator extends javax.mail.Authenticator
{
private String userName;
private String password;
public SMTPAuthenticator(String username,String password)
{
this.userName=username;
this.password=password;
}
/** Creates a new instance of SMTPAuthenticator */
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName,password);
}
}
auth = new SMTPAuthenticator(username, password);
session = Session.getDefaultInstance(mailProperties, auth);
ok that sounds a bit more understandable. however, how do i drop this into my jsp code? appologies as i am very new to java.
i have my jsp with the code on already, so im guessing i need to embed this new code but im not sure on how to use it?
my code is:
<%@page import="java.util.*"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
Session s = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress("crosskeysconstruction@googlemail.com");
message.setFrom(from);
InternetAddress to = new InternetAddress("suppliercompany@googlemail.com");
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Test from JavaMail.");
message.setText("Hello from JavaMail!");
Transport.send(message);
%>
A Message has been sent.
Check your inbox.
<a href="sendmail.jsp">Click here to send another!</a>
1) give a nice package name and compile SMTPAuthenticator.java
2) Make a jar. (If your package name is com.test then you can make the jar using the command
jar -cvfM auth.jar com [assuming 'com' is in the current directory.])
3) add this jar to WEB-INF\lib
4) then you can import <%@page import="com.test.SMTPAuthenticator"%>
5) Now you know the rest...
I simply fail to understand, why do people keep on puting code in the jsp's.
Keep jsp's for the view!!!!!!!!