Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending email with content from web page & database

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this one shouldn't be too bad...
Using JSP (or a servlet), how would I send (submit) an email to someone with content that originates on a web page (input)?
And how could I send an email with content from a database?
It's the actual sending of the email I'm not sure about, and the loading of its content.
I know how to get the data into the database (JDBC). FYI, I'm using Tomcat and a MySQL database. Thanks.
Cheers,
Scott
 
David Freels
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out javamail at http://www.javasoft.com/products/javamail
David
Sun Certified Programmer for the Java2 Platform
 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three easy ways:
1. SMTP using Sockets
Open a socket to your mail host and SMTP port number. If your ISP is foo.com, the mail host is usually mail.foo.com or something close. The port number is 25 by default. You can test this with telnet:

If this works, send these lines as ordinary text:

You'll get a response line after each of these. The return code is numeric and is contained in the first three bytes.
Obviously, I'm simplifying it a little. See RFC 821 (Simple Mail Transfer Protocol) for complete details at http://www.freesoft.org/CIE/RFC/821/index.htm .
2. Use the sun.net.smtp.SmtpClient class
This is marginally simpler, being an object-oriented wrapper around the bare socket protocol. There are two big drawbacks:
- It is undocumented (although not hard to figure out)
- Sun warns you that they can change or drop the class
3. Use JavaMail as previously noted.
Why bother with options 1 or 2 if option 3 is the preferred choice? Because it isn't difficult and it's worth learning what goes on under the covers. For a production mail system, obviously, you need the industrial strength option, but for simple applications, the socket-based approach may be all you need.
(This material is discussed at greater length in Chapter 21).
------------------
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets
[This message has been edited by Phil Hanna (edited April 24, 2001).]
[This message has been edited by Phil Hanna (edited April 24, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Phil!
Maybe I'll have to add your book to my collection
Cheers,
Scott
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic