Java Forum / General / October 2006
who can give a simple example? Urgently need!!
Flamingo - 19 Oct 2006 15:37 GMT 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.
Oliver Wong - 19 Oct 2006 16:45 GMT > 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:gsuxin@gmail.com", for example.
- Oliver
Flamingo - 19 Oct 2006 17:18 GMT 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!
> > Hi folks, > > [quoted text clipped - 13 lines] > > - Oliver Oliver Wong - 19 Oct 2006 17:27 GMT [post re-ordered]
>> > Hi folks, >> > [quoted text clipped - 18 lines] > client application such as outlook, I want it directly be sent. How it > works! thanks! See the part before "Alternatively, ".
- Oliver
Manish Pandit - 19 Oct 2006 19:06 GMT 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
RedGrittyBrick - 19 Oct 2006 20:53 GMT > 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.
Simon Brooke - 19 Oct 2006 22:57 GMT > 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'.
 Signature simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
Hobbit ringleader gives Sauron One in the Eye.
Arne Vajhøj - 20 Oct 2006 03:05 GMT >> 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'. Can you see in your crystal ball that the poster is not using JSP ?
Arne
Andrew Thompson - 20 Oct 2006 05:35 GMT > >> I want a webpage, there would be a input textfield, and a "sumbit" > >> there, if you press the submit button, ....
> > 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 ? 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.
Oliver Wong - 20 Oct 2006 15:22 GMT > > > in message <1161268631.340033.304280@i3g2000cwc.googlegroups.com>, > > > Flamingo [quoted text clipped - 12 lines] > 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
Simon Brooke - 20 Oct 2006 22:46 GMT >> Arne Vajhj wrote: >> > > in message <1161268631.340033.304280@i3g2000cwc.googlegroups.com>, [quoted text clipped - 22 lines] > 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/*checkout*/pres/pres/src/java/uk/co/weft/pres/se rver/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.
 Signature simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; no eternal reward will forgive us now for wasting the dawn. ;; Jim Morrison
Arne Vajhøj - 21 Oct 2006 18:17 GMT >>>> I want a webpage, there would be a input textfield, and a "sumbit" >>>> there, if you press the submit button, [quoted text clipped - 3 lines] > > 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
Simon Brooke - 20 Oct 2006 09:42 GMT >>> 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 [quoted text clipped - 4 lines] > > 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.
 Signature simon@jasmine.org.uk (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
Arne Vajhøj - 21 Oct 2006 18:22 GMT >>>> 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 [quoted text clipped - 10 lines] > > 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
Simon Brooke - 22 Oct 2006 16:59 GMT >>>>> 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 [quoted text clipped - 16 lines] > 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.
 Signature simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; An enamorata is for life, not just for weekends.
Arne Vajhøj - 23 Oct 2006 01:58 GMT >> I seem to be missing something. >> [quoted text clipped - 4 lines] > 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.
>> 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. If it for whatever reason are to be coded in java then it is 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 [quoted text clipped - 3 lines] > 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
Oliver Wong - 23 Oct 2006 15:24 GMT >> 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 think there's a good chance the OP might have been thinking of (D)HTML+JavaScript when (s)he posted here.
- Oliver
Arne Vajhøj - 24 Oct 2006 00:03 GMT >>> 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. [quoted text clipped - 4 lines] > I think there's a good chance the OP might have been thinking of > (D)HTML+JavaScript when (s)he posted here. That is a possibility.
And it would have been perfectly valid to ask for verification.
I am objecting stating as a fact that it was the wrong group.
Arne
IchBin - 20 Oct 2006 00:29 GMT > 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.
 Signature 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-)
Wibble - 20 Oct 2006 03:00 GMT >> Hi folks, >> [quoted text clipped - 5 lines] > Personally for what you want to do I would do it in php. you could do it > in less then 10 lines of code. I second that, you can even use a free php server like
www.xlphp.net
Charles Hottel - 20 Oct 2006 01:39 GMT > 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(); } }
Arne Vajhøj - 20 Oct 2006 03:09 GMT > 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 = "mig@xxx.dk"; public final static String TO = "mig@xxx.dk"; 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); } %>
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|