who can give a simple example? Urgently need!!

F

Flamingo

Hi folks,

I want a webpage, there would be a input textfield, and a "sumbit"
there, if you press the submit button, the contents you input to the
textfield would be send to your email. who can give me a simple sample
about that. thanks a lot. I really need i t urgently.
 
O

Oliver Wong

Flamingo said:
Hi folks,

I want a webpage, there would be a input textfield, and a "sumbit"
there, if you press the submit button, the contents you input to the
textfield would be send to your email. who can give me a simple sample
about that. thanks a lot. I really need i t urgently.

If you're gonna do it using Java, you'd probably need JSPs and J2EE and
all that stuff, and it wouldn't be simple. You'd probably have better luck
doing it in JavaScript, which means you should ask in a JavaScript
newsgroup.

Alternatively, if you're not too picky about how the e-mail will be
formatted, you could do it all in HTML. Just fill the "action" attribute of
the form with "mailto:[email protected]", for example.

- Oliver
 
F

Flamingo

Hi, Oliver,

I don't want to use "mailto", because it would open some webmail
client application such as outlook, I want it directly be sent. How it
works! thanks!
 
O

Oliver Wong

[post re-ordered]

Flamingo said:
I don't want to use "mailto", because it would open some webmail
client application such as outlook, I want it directly be sent. How it
works! thanks!

See the part before "Alternatively, ".

- Oliver
 
M

Manish Pandit

What you need is a text area, a servlet and java mail API.

1. Have the form in the HTML post to the servlet, which can then use
Java Mail API to send the contents of the text area in an email. You
can get the text by using request.getParameter("fieldname").

2. If you want a cleaner and reusable approach, put the email sending
logic in a util class and invoke it from the servlet.

3. If you do not feel comfortable with servlets, post to a JSP that
does the same stuff as above using scriptlets.

4. If you are using struts, do (2) in an action class.

-cheers,
Manish
 
R

RedGrittyBrick

Flamingo said:
Hi folks,

I want a webpage, there would be a input textfield, and a "sumbit"
there, if you press the submit button, the contents you input to the
textfield would be send to your email. who can give me a simple sample
about that. thanks a lot. I really need i t urgently.

My first thought was FormMail in
http://nms-cgi.sourceforge.net/scripts.shtml

Then I realised what newsgroup I was in.
 
S

Simon Brooke

Flamingo said:
Hi folks,

I want a webpage, there would be a input textfield, and a "sumbit"
there, if you press the submit button, the contents you input to the
textfield would be send to your email. who can give me a simple sample
about that. thanks a lot. I really need i t urgently.

This isn't even remotely a Java question. Google for 'cgiemail'.
 
I

IchBin

Flamingo said:
Hi folks,

I want a webpage, there would be a input textfield, and a "sumbit"
there, if you press the submit button, the contents you input to the
textfield would be send to your email. who can give me a simple sample
about that. thanks a lot. I really need i t urgently.
Personally for what you want to do I would do it in php. you could do it
in less then 10 lines of code.

--
Thanks in Advance... http://ichbinquotations.awardspace.com
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
C

Charles Hottel

Flamingo said:
Hi folks,

I want a webpage, there would be a input textfield, and a "sumbit"
there, if you press the submit button, the contents you input to the
textfield would be send to your email. who can give me a simple sample
about that. thanks a lot. I really need i t urgently.

Here is an example from Peter van der Linden's Just Java 6th edition page
659. I hope it will get you started and you can figure out the rest.

import java.io.*;
import java.net.*;
public class email {

public static void main(String args[]) throws IOException {
Socket sock = null;
BufferedReader bis = null;
PrintStream ps = null;

try {
sock = new Socket("localhost", 25);
bis = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
ps = new PrintStream(sock.getOutputStream());

ps.println("mail from: trelford");
//System.out.println( dis.readLine() );
// Exercise for student:
// check all responses from the SMTP server
// They should all start with "2nn" or "3nn"
// Any different reply code means a failure to send mail.
System.out.println(bis.readLine());
} catch (IOException ioe) {
System.out.println("Sorry, your system is not running a
mailserver on port 25");
System.exit(0);
}

String Addressee= "linden";
ps.println("rcpt to: " + Addressee);
//System.out.println( dis.readLine() );
System.out.println(bis.readLine());

ps.println("data");
//System.out.println( dis.readLine() );
System.out.println(bis.readLine());

ps.println("This is the message\n that Java sent");
ps.println(".");
System.out.println(bis.readLine());

ps.flush();
sock.close();
}
}
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Simon said:
This isn't even remotely a Java question. Google for 'cgiemail'.

Can you see in your crystal ball that the poster is not using JSP ?

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Flamingo said:
I want a webpage, there would be a input textfield, and a "sumbit"
there, if you press the submit button, the contents you input to the
textfield would be send to your email. who can give me a simple sample
about that. thanks a lot. I really need i t urgently.

I will assume that you mean a Java web app.

I will also assume that you are relative new
to Java web apps.

Below are some JSP code that can be be specified as action
for a HTML form. From 0 to 10 fields in the HTML form are
then emailed.

Arne

<%@page import="java.util.*,javax.mail.*,javax.mail.internet.*"%>
<%!
/*
* Configure mail here.
*/
public final static String MAILSERVER = "mail.xxx.dk";
public final static String FROM = "(e-mail address removed)";
public final static String TO = "(e-mail address removed)";
public final static String SUBJECT = "Test";
/*
* Configure pages to go to.
*/
public static final String SUCCESS = "form.html";
public static final String FAILURE = "form.html";
/*
* Configure fields here.
*/
public static final String FIELD1 = "Kæde";
public static final String FIELD2 = "Firma";
public static final String FIELD3 = "Adresse";
public static final String FIELD4 = "Postnr";
public static final String FIELD5 = "By";
public static final String FIELD6 = "Mail";
public static final String FIELD7 = "Telefon";
public static final String FIELD8 = "Fax";
public static final String FIELD9 = "Att";
public static final String FIELD10 = "";
%>
<%!
public static boolean sendEmail(String mailserver, String from,
String to, String subject, String body) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", mailserver);
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
} catch (AddressException e) {
return false;
} catch (MessagingException e) {
return false;
}
return true;
}
%>
<%
if(request.getMethod().equals("POST")) {
StringBuffer formdata = new StringBuffer("");
if(request.getParameter(FIELD1) != null) {
formdata.append(FIELD1 + ": " + request.getParameter(FIELD1) +
"\n");
}
if(request.getParameter(FIELD2) != null) {
formdata.append(FIELD2 + ": " + request.getParameter(FIELD2) +
"\n");
}
if(request.getParameter(FIELD3) != null) {
formdata.append(FIELD3 + ": " + request.getParameter(FIELD3) +
"\n");
}
if(request.getParameter(FIELD4) != null) {
formdata.append(FIELD4 + ": " + request.getParameter(FIELD4) +
"\n");
}
if(request.getParameter(FIELD5) != null) {
formdata.append(FIELD5 + ": " + request.getParameter(FIELD5) +
"\n");
}
if(request.getParameter(FIELD6) != null) {
formdata.append(FIELD6 + ": " + request.getParameter(FIELD6) +
"\n");
}
if(request.getParameter(FIELD7) != null) {
formdata.append(FIELD7 + ": " + request.getParameter(FIELD7) +
"\n");
}
if(request.getParameter(FIELD8) != null) {
formdata.append(FIELD8 + ": " + request.getParameter(FIELD8) +
"\n");
}
if(request.getParameter(FIELD9) != null) {
formdata.append(FIELD9 + ": " + request.getParameter(FIELD9) +
"\n");
}
if(request.getParameter(FIELD10) != null) {
formdata.append(FIELD10 + ": " + request.getParameter(FIELD10)
+ "\n");
}
if(sendEmail(MAILSERVER, FROM, TO, SUBJECT, formdata.toString())) {
response.sendRedirect(SUCCESS);
} else {
response.sendRedirect(FAILURE);
}
} else {
response.sendRedirect(FAILURE);
}
%>
 
A

Andrew Thompson

Arne said:
Can you see in your crystal ball that the poster is not using JSP ?

How would that change *anything*?

The answer I'd have given an OP using JSP is that 'you
might do this exactly the same way you do it in (D)HTML',
which crudely translates to 'if it arrives in a browser as
pure HTML - ask those questions on an HTML group'.

Andrew T.
 
S

Simon Brooke

Arne Vajhøj said:
Can you see in your crystal ball that the poster is not using JSP ?

I can't see anything in my crystal ball. Rolling your own webform-to-
email solution is fraught with a lot of subtle security problems, and is
not for beginners. cgiemail, although not a Java solution, is nevertheless
a well engineered, well tested, secure solution which is available off the
shelf for free.

Don't reinvent wheels.

--
(e-mail address removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/
; gif ye hes forget our auld plane Scottis quhilk your mother lerit you,
; in tymes cuming I sall wryte to you my mind in Latin, for I am nocht
; acquyntit with your Southeron
;; Letter frae Ninian Winyet tae John Knox datit 27t October 1563
 
O

Oliver Wong

Andrew Thompson said:
How would that change *anything*?

The answer I'd have given an OP using JSP is that 'you
might do this exactly the same way you do it in (D)HTML',
which crudely translates to 'if it arrives in a browser as
pure HTML - ask those questions on an HTML group'.

Perhaps this is a homework assignment, and the teacher is requiring the
OP to write a JSP which processes the data sent in the form, and telnets
into an SMTP server to send out the mail.

IOW, it's possible to do this purely in Java, but if you have the
choice, you'd probably be better off using a different tool than Java for
this particular problem.

- Oliver
 
S

Simon Brooke

Oliver Wong said:
Perhaps this is a homework assignment, and the teacher is requiring
the
OP to write a JSP which processes the data sent in the form, and telnets
into an SMTP server to send out the mail.

IOW, it's possible to do this purely in Java, but if you have the
choice, you'd probably be better off using a different tool than Java for
this particular problem.

That isn't what I'm saying at all. If I had to do this from scratch I
definitely would do it as a Servlet using java.mail - indeed I have built
Servlets which generate email by filling in templates (e.g. when sending
password reminders:
http://pres.cvs.sourceforge.net/*ch.../uk/co/weft/pres/server/AbstractMailPass.java
), which is essentially what we're talking about here. However, if you want
a generic solution to generating mail from HTML forms, there are existing
engineered solutions out there, and, IME, cgiemail - which I have been
using now for more than ten years - is one of the best.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Andrew said:
How would that change *anything*?

JSP is Java, so processing a form submit by a JSP page (or
probably more suitable a servlet) makes it Java.
The answer I'd have given an OP using JSP is that 'you
might do this exactly the same way you do it in (D)HTML',
which crudely translates to 'if it arrives in a browser as
pure HTML - ask those questions on an HTML group'.

Sending the mail client side and server side is not
the same.

If the original poster asked in a HTML group about
how to write some Java code that processed a submitted
form, then I think he would be pointed back here immediately.

Arne
 
G

Guest

Simon said:
I can't see anything in my crystal ball. Rolling your own webform-to-
email solution is fraught with a lot of subtle security problems, and is
not for beginners. cgiemail, although not a Java solution, is nevertheless
a well engineered, well tested, secure solution which is available off the
shelf for free.

Don't reinvent wheels.

I seem to be missing something.

How did you get from "cgiemail ... is nevertheless a well engineered,
well tested, secure solution which is available off the shelf for free.
Don't reinvent wheels," to "This isn't even remotely a Java question." ?
Is it fundamenttaly impossible to write such code in Java. Or have you
checked all Java code repositories and know for certain that there
are no good solutions in Java ?

And after reading from http://web.mit.edu/wwwdev/cgiemail/webmaster.html
it seems as if cgiemail is Unix only, which raises the question
whether you crystal ball showed that the original poster where
running Unix.

Arne
 
S

Simon Brooke

Arne Vajhøj said:
I seem to be missing something.

How did you get from "cgiemail ... is nevertheless a well engineered,
well tested, secure solution which is available off the shelf for free.
Don't reinvent wheels," to "This isn't even remotely a Java question." ?

The OP said he wanted to get stuff typed into an HTML form sent as email.
Nothing about the problem as stated is specific to Java.
Is it fundamenttaly impossible to write such code in Java.

Certainly not. As I posted in the message which you replied to, I have
personally written very similar code in Java. That doesn't make it a Java
problem.
Or have you
checked all Java code repositories and know for certain that there
are no good solutions in Java ?

I didn't say there were no good solutions in Java. Indeed, I know there
are. All I said was, it isn't a Java problem.
And after reading from http://web.mit.edu/wwwdev/cgiemail/webmaster.html
it seems as if cgiemail is Unix only, which raises the question
whether you crystal ball showed that the original poster where
running Unix.

If he's running a web server on Windows, email is a long way down his list
of problems.
 
G

Guest

Simon said:
The OP said he wanted to get stuff typed into an HTML form sent as email.
Nothing about the problem as stated is specific to Java.

Sure did.

The choice of news group.
I didn't say there were no good solutions in Java. Indeed, I know there
are. All I said was, it isn't a Java problem.

If it for whatever reason are to be coded in java then it is a java
problem.
If he's running a web server on Windows, email is a long way down his list
of problems.

Well - if you think that not Unix means Windows, then I don't think
you are qualified to comment on OS capabilities.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top