Java Forum / First Aid / November 2004
sending e-mail from server
Frances Del Rio - 23 Nov 2004 20:11 GMT I want to put a java e-mail form on my website that sends e-mail to an address specified (my address, but can also be someone else's, like for 'send pg to a friend' link...) in order to protect my address from spammers I don't want to put e-mail address in hidden field in form, I want e-mail address to appear only on server.. this is very easy to do c/CGI... but I get the feeling it's a bit more complicated w/java... I need javax.mail, right? anything else? why is this class not included in system API? what a pain.. b/c will have to put in my own tomcat locally (to test or whatever..) and then also up where my site is hosted.. would appreciate suggestions.. thank you.. Frances
kaeli - 24 Nov 2004 15:41 GMT > I want to put a java e-mail form on my website that sends e-mail to an > address specified (my address, but can also be someone else's, like for [quoted text clipped - 6 lines] > locally (to test or whatever..) and then also up where my site is > hosted.. would appreciate suggestions.. thank you.. Frances Actually, you don't NEED javamail. Javamail has a ton of options and stuff that a simple mail might not need. You can use plain and simple smtp mail. It's undocumented, so here's a snippet from my stuff, edited for Usenet.
<%@ page import="sun.net.smtp.SmtpClient" %> <% SmtpClient client = new SmtpClient("your.mail.host"); client.from("some value"); client.to("some value"); PrintStream message = client.startMessage(); message.println("Content-Type: text/plain"); message.println("To: some value"); message.println("Subject: whatever"); message.println(); message.println("some message"); message.println(); client.closeServer(); %>
 Signature -- ~kaeli~ Murphy's Law #2000: If enough data is collected, anything may be proven by statistical methods. http://www.ipwebdesign.net/wildAtHeart http://www.ipwebdesign.net/kaelisSpace
Frances Del Rio - 24 Nov 2004 16:52 GMT thank you!!
>>I want to put a java e-mail form on my website that sends e-mail to an >>address specified (my address, but can also be someone else's, like for [quoted text clipped - 25 lines] > client.closeServer(); > % Frances Del Rio - 24 Nov 2004 18:10 GMT >>I want to put a java e-mail form on my website that sends e-mail to an >>address specified (my address, but can also be someone else's, like for [quoted text clipped - 25 lines] > client.closeServer(); > %> kaeli, again, thank you very much.. where does this go, exactly? in form? so do a form.jsp and put this code in there? then just direct folks to a 'thank you' page? I mean when they hit 'submit'? thank you again.. Frances
Anzime - 26 Nov 2004 21:06 GMT >>> I want to put a java e-mail form on my website that sends e-mail to >>> an address specified (my address, but can also be someone else's, [quoted text clipped - 32 lines] > folks to a 'thank you' page? I mean when they hit 'submit'? thank you > again.. Frances That scriptlet goes in a JSP. So, you have to have a page with a form action that submits to the JSP with the scriplet in it, then after the scriptlet executes you can display a "Thank you" or send the user to a "Thank you" page.
BTW, you may want to use mail.jar and activation.jar instead of the undocumented sun classes because you can't count on the sun classes to be there in upcoming JDK releases.
 Signature Regards, Anzime
kaeli - 29 Nov 2004 16:14 GMT > > <%@ page import="sun.net.smtp.SmtpClient" %> > > <% [quoted text clipped - 13 lines] > kaeli, again, thank you very much.. where does this go, exactly? in > form? Actually, I use one page for the form (.html), and then one page as the form handler (.jsp). Keeps it simple for me to debug and the like. Plus, sometimes I use a bean as the handler. This code would go in the handling page, either as is or made into a bean. I can post the bean if you'd like.
Note that the sun classes are not documented and may not be in upcoming releases. If you like them, be prepared to grab the jar from an old SDK when you upgrade. ;)
So, you have an html page with a form and a jsp page as the action. The code goes in the action page, not the form page, assuming you also keep them separate. Some people put it all in one page and check the request method. I find it harder to debug that way (or modify, such as when I changed all my stuff to beans), but it's a personal opinion.
Here's another example, both the form and the handler, to clarify. Note that it has been modified a bit for Usenet posting. These are snippets. Some variables may be missing declarations or whatnot. Don't expect to just be able to copy and paste it. :)
from register.html: --------------- <form name="f1" action="registerNow.jsp" method="post" onSubmit="return validateMe(this);"> <table class="tRegistration"> <tr> <th>First Name: </th> <td><input type="text" name="fname" size="30" maxlength="49"> </td> </tr> <tr> <th>Last Name:</th> <td><input type="text" name="lname" size="30" maxlength="49"></td> </tr> <tr> <th>HRID:</th> <td><input name="hrid" type="text" id="hrid" size="20" maxlength="10"> </td> </tr> <tr> <th>Phone Number: <span class="small">(555-555-5555)</span></th> <td><input type="text" name="phone" size="20" maxlength="20"></td> </tr> <tr> <th>E-mail:<span class="small"> (handle@domain.com)</span></th> <td><input type="text" name="email" size="30" maxlength="99"></td> </tr> </table> <table class="tRegistration"> <tr> <th>Please select the workshop you'd like to attend.</th> <td><select name="wid"> <option value='none' selected>-- Choose one --</option> <option value='first'>first</option> <option value='second'>second</option> </select> </td> </tr> <tr> <th>Would you like to attend the networking event? <span class="small"> (no additional charge)</span> </th> <td><input type="radio" name="networking" value="N">No<br> <input type="radio" name="networking" value="Y">Yes </td> </tr> <tr> <td align="center"><input name="reset" type="reset" id="reset" value="Reset"> </td> <td align="center"><input type="submit" name="Submit" value="Submit"> </td> </tr> </table> <p></p> </form> ---------------
from registerNow.jsp: (elipses (...) means code has been snipped)
---------------
<%@ page import="sun.net.smtp.SmtpClient" %> ...
try { // get params hrid=request.getParameter("hrid"); if (hrid == null || hrid.length() == 0) throw new IllegalArgumentException ("Missing field: HRID. Please go back and fill in your HRID, then try again."); fname=request.getParameter("fname"); if (fname == null || fname.length() == 0) throw new IllegalArgumentException("Missing field: First Name. Please go back and fill in your first name, then try again."); lname=request.getParameter("lname"); if (lname == null || lname.length() == 0) throw new IllegalArgumentException("Missing field: Last Name. Please go back and fill in your last name, then try again."); phone=request.getParameter("phone"); if (phone == null || phone.length() == 0) throw new IllegalArgumentException("Missing field: Phone Number. Please go back and fill in your phone number, then try again."); email=request.getParameter("email"); if (email == null || email.length() == 0) throw new IllegalArgumentException("Missing field: Email. Please go back and fill in your email, then try again."); tmpString=request.getParameter("wid"); if (tmpString == null || tmpString.equals("none")) throw new IllegalArgumentException("Missing field: Workshop. Please go back and choose a workshop, then try again."); else wid = Integer.parseInt(tmpString); networking=request.getParameter("networking"); if (networking == null || networking.length() == 0) throw new IllegalArgumentException("Missing field: Networking. Please go back and fill in your networking choice (yes or no), then try again."); ... {snipped code that looks up workshop name (wname) for param wid in database and checks other stuff} ...
// send off mail String ccMailRecipients = "{edited for usenet}"; String from="{edited for usenet}"; String to=email+","+ccMailRecipients; SmtpClient client = new SmtpClient("{edited}"); client.from(from); client.to(to); PrintStream message = client.startMessage(); message.println("To: " + email); message.println("Subject: Registration Confirmation"); message.println(); message.println("Attention: "+fname+ " "+lname+"\n"); message.println("Thanks for registering for the Development Conference.\n"); message.println("You have registered for the following workshop:"); message.println(wname); tmpString = networking.equals("Y")?"are":"are not"; message.println("You " + tmpString + " registered for the optional networking activity.\n"); message.println("To complete the registration process, you MUST pay your "+ "$10 registration fee."); message.println("Thanks,\nRegistration Team"); message.println(); client.closeServer(); %> <p><b>Thanks for registering! </b>An e-mail should arrive in your inbox with further details shortly.</p> <% } catch (Exception e) { String str = e.getMessage(); if (str.indexOf("unique constraint") > -1) { %> <p class='error'>That HRID is already registered.</p> <% } else out.println("<p class='error'>"+str+"</p>"); } finally { try { if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (Exception e2) {} } %> <p></p> ------------------
HTH
 Signature -- ~kaeli~ If it's tourist season, why can't we shoot them? http://www.ipwebdesign.net/wildAtHeart http://www.ipwebdesign.net/kaelisSpace
Frances Del Rio - 29 Nov 2004 19:15 GMT >>><%@ page import="sun.net.smtp.SmtpClient" %> >>><% [quoted text clipped - 34 lines] > Some variables may be missing declarations or whatnot. Don't expect to just > be able to copy and paste it. :) ok, I have actually added a few necesary things... this is what my jsp basically looks like:
<%@ page import="sun.net.smtp.SmtpClient" %> <html> <head> <title> </title> </head> <body> <br><br><br><br> <center> <% try {
// get params hrid=request.getParameter("hrid");
etc..
I put in e-mail addresses, name of server, etc.. but the weird thing is I keep getting errors like these:
An error occurred at line: 9 in the jsp file: /mail.jsp Generated servlet error: C:\tomcat\work\Catalina\localhost\mail\org\apache\jsp\mail_jsp.java:58: unclosed string literal ("Missing field: HRID. Please go back and fill in your HRID, then try ^
An error occurred at line: 9 in the jsp file: /mail.jsp Generated servlet error: C:\tomcat\work\Catalina\localhost\mail\org\apache\jsp\mail_jsp.java:59: unclosed string literal again."); ^
this is so weird, don't know what to make of it... tons of errors like this.. ok, either way thank you very much for taking the time... (it turns out I do have the javamail classes avail. to me at my webhosting, do you recommend using that instead of sun.net.smtp.SmtpClient if I have a choice?) for right now just need a simple e-mail form where e-mail is sent to me and another one for 'send-pg-to-a-friend' form.. thanks again... Frances
Oscar kind - 29 Nov 2004 20:55 GMT > I put in e-mail addresses, name of server, etc.. but the weird thing is > I keep getting errors like these: [quoted text clipped - 5 lines] > ("Missing field: HRID. Please go back and fill in your HRID, then try > ^ This means some string literal (such as "abc") is unclosed, i.e. missing a closing double quote. Since the error is for line 9, I'd start searching from the end of line 9 backwards and find the culprit.
 Signature Oscar Kind http://home.hccnet.nl/okind/ Software Developer for contact information, see website
PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
kaeli - 30 Nov 2004 14:16 GMT > I put in e-mail addresses, name of server, etc.. but the weird thing is > I keep getting errors like these: [quoted text clipped - 5 lines] > ("Missing field: HRID. Please go back and fill in your HRID, then try > ^ That just means you forgot a closing quote or have text on two lines when it should be on one. The rest of the errors may be all because of that, so go fix that, then try again.
> (it turns out I do have the javamail classes avail. to me at my > webhosting, do you recommend using that instead of > sun.net.smtp.SmtpClient if I have a choice?) For just one simple task? I would say you should get familiar with both options, as the sun classes may be deprecated, but it won't make a huge difference for something simple. I don't have the javamail classes and I didn't feel like downloading and installing them, nor do I need them, so I didn't bother. If I'd had them available off the bat, I'd have probably used them. Scratch the probably. I would have used them.
 Signature -- ~kaeli~ Why do they lock gas station bathrooms? Are they afraid someone will clean them? http://www.ipwebdesign.net/wildAtHeart http://www.ipwebdesign.net/kaelisSpace
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 ...
|
|
|