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]
# 1

>Properties props = new Properties();

use this instead:

Properties props = System.getProperties();

http://www.java2s.com/Code/JavaDownload/SendEmailFromJsp.zip

java_2006a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 2

still no luck just the same error and unrecognised syntaxes.

haggard17a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 3

> 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.

prassoona at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 4

>I have imported the libraries for javamail.jar and jaf.jar but still no luck.

how ?

add these jars to WEB-INF/lib/

java_2006a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 5

i added it through jbuilder>>required libraries but i think it must still be the problem.

any ideas how to ressolve?

haggard17a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 6

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?

haggard17a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 7

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?

haggard17a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 8

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?

prassoona at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 9

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?

haggard17a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 10

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.

prassoona at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 11

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?

haggard17a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 12

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);

prassoona at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 13

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>

haggard17a at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 14

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...

prassoona at 2007-7-29 14:02:31 > top of Java-index,Java Essentials,New To Java...
# 15

I simply fail to understand, why do people keep on puting code in the jsp's.

Keep jsp's for the view!!!!!!!!

kilyasa at 2007-7-29 14:02:35 > top of Java-index,Java Essentials,New To Java...