Hi,

her is a servlet skeleton to send mail from a servlet by connecting to a
mail server (no JavaMail).

Again, this example is taken from my book "Servlet et Java Server Pages - Le
guide du développeur" (published in french);

As is, it will probably not compile because I have taken off all the stuff
concerning getting teh data from a database trough JDBC.

Note that parameter are to be red from a property file, so that the servlet
is somewhat portable.

I have introduce a few words of comments to translate the methods name in
english.

I hope it helps !

Pierre-Yves

public class MailServlet extends HttpServlet {

        String propFileName = "default.prop";

        public void service (HttpServletRequest requete,
                                HttpServletResponse reponse) throws ServletException, 
IOException{

                String path = getServletConfig().getServletContext().getRealPath("/");
                reponse.setContentType("text/html");
                PrintWriter pw = reponse.getWriter();
                Properties properties = new Properties();
                try {
                        FileInputStream propFile = new FileInputStream(path + 
propFileName);
                        properties.load(propFile);
                }
                catch (IOException ioe) {
                        System.out.println(ioe);
                        return;
                }
                        .
                        .
                        .

                String expe = properties.getProperty("expe"); // expe = sender
                String dest = properties.getProperty("dest"); // dest is the adress 
wher
you are sending mail
                String sujet = properties.getProperty("mailSubject");
                StringBuffer buffer = new StringBuffer();
                StringBuffer message = new StringBuffer();

                message.append("Hi,\n\n");
                message.append("This is the text of the message...\n");
                message.append("....\n");
                message.append(".....\n");
                message.append("Best regards\n\n");

                String serveur = properties.getProperty("mailServer");
                Socket socket = new Socket(serveur, 25);

                PrintWriter mailWriter
                                        = new PrintWriter(socket.getOutputStream(), 
true);
                BufferedReader mailReader
                                        = new BufferedReader(new InputStreamReader(
                                                socket.getInputStream()));

                connecter(serveur, mailWriter, mailReader, buffer); // buffer wil 
contains
mailserver response (usefull for tracking problems)
                envoyerCommande("MAIL FROM:", expe, mailWriter, mailReader, buffer);
                envoyerCommande("RCPT TO:", expe, mailWriter, mailReader, buffer);
                envoyerCommande("DATA", "", mailWriter, mailReader, buffer);
                envoyerDonnées(dest, sujet, message.toString(), mailWriter, mailReader,
buffer);
                fermer(socket, mailWriter, mailReader, buffer);

                //pw.println(buffer.toString()); // This is for tracking problems with 
teh
server
                        }
                }

        }
        private void connecter( // connecter = connect
                                        String serveur,
                                        PrintWriter mailWriter,
                                        BufferedReader mailReader,
                                        StringBuffer buffer)
                                        throws IOException
        {
                buffer.append("<p>" + mailReader.readLine());
                buffer.append("HELO " + serveur + "\r\n");
                mailWriter.print("HELO " + serveur + "\r\n");
                mailWriter.flush();
                buffer.append("<br>" + mailReader.readLine());
        }

        private void envoyerCommande( // envoyerCommande = send command
                                        String commande,
                                        String paramètre,
                                        PrintWriter mailWriter,
                                        BufferedReader mailReader,
                                        StringBuffer buffer)
                                        throws IOException
        {
                buffer.append("<br>" + commande + paramètre + "\n");
                if (!paramètre.equals(""))
                {
                        paramètre = "<" + paramètre + ">";
                }
                mailWriter.print(commande + paramètre + "\r\n");
                mailWriter.flush();
                buffer.append("<br>" + mailReader.readLine());
        }

        private void envoyerDonnées( // envoyerDonnées = send data
                                        String dest,
                                        String sujet,
                                        String message,
                                        PrintWriter mailWriter,
                                        BufferedReader mailReader,
                                        StringBuffer buffer)
        {
                buffer.append("to: " + dest + "\r\n");
                mailWriter.print("to: " + dest + "\r\n");
                buffer.append("subject: " + sujet + "\r\n");
                mailWriter.print("subject: " + sujet + "\r\n");
                buffer.append(message + "\r\n");
                mailWriter.print(message + "\r\n");
        }

        private void fermer( // fermer = shut
                                        Socket socket,
                                        PrintWriter mailWriter,
                                        BufferedReader mailReader,
                                        StringBuffer buffer)
                                        throws IOException
        {
                mailWriter.print("\r\n.\r\n");
                mailWriter.flush();
                buffer.append("<br>" + mailReader.readLine());
                buffer.append("<br>" + "QUIT" + "\n");
                mailWriter.print("QUIT" + "\r\n");
                mailWriter.flush();
                buffer.append("<br>" + mailReader.readLine());
                socket.close();
        }
}

-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de roy
woods
Envoyé : jeudi 22 mars 2001 13:48
À : [EMAIL PROTECTED]
Objet : sending email from html form? how


Hi all

How can I send an email from html form? I know I
should have the JavaMail package first and should know
the name of the SMTP. what else do I need to have?
Could someone be kind enough to post a simple code
that sends the an email to the email especified in the
form with the content in the form's textarea?

thanks in advance

Roy--

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://personal.mail.yahoo.com/

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to