Hi : write a servlet X when the user submits the form .Before post all the
information like To from , subject , contents etc etc. Create a class
sendmail.java . In servlet X , use this sendmail to send to a particular
user and also you can write code to stroe in the DB,

Sample send mail is

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

/**
 * sendmail creates a simple multipart/alternate message and sends it.
 * Body parts are text/plain and text/html
 * <p>
 * usage: <code>java sendmail <i>to from smtp true|false</i></code>
 * where <i>to</i> and <i>from</i> are the destination and
 * origin email addresses, respectively, and <i>smtp</i>
 * is the hostname of the machine that has smtp server
 * running.  The last parameter either turns on or turns off
 * debugging during sending.
 *
 * @author      Asha
 */
public class sendmail {
                           ..... code main and variable initialization

        // create some properties and get the default Session
        Properties props = new Properties();
        props.put(SMTPHOST,m_host);

        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);

        try {
            // create a message
            MimeMessage msg = new MimeMessage(session);
            // set from
            msg.setFrom(new InternetAddress(from));
            // set to as a array of addresses , for us one only
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            mg.setSentDate(new Date());

            // create and fill the first message body part1 and part2
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(msgText1);

        // prepare html content
        StringBuffer sb = new StringBuffer();
            sb.append("<HTML>\n");
            sb.append("<HEAD>\n");
            sb.append("<TITLE>\n");
            sb.append("JavaMail APIs Multipart Test" + "\n");
            sb.append("</TITLE>\n");
            sb.append("</HEAD>\n");


            sb.append("<BODY>\n");
            sb.append("<H1>" + "Test Mail for sending HTML in mail" +
"</H1>" + "\n");
          sb.append("</BODY>\n");
            sb.append("</HTML>\n");

            // create and fill the second message part
            MimeBodyPart mbp2 = new MimeBodyPart();

            // pass DataHandler object wrapped with message body
            mbp2.setDataHandler(new DataHandler(new
ByteArrayDataSource(sb.toString(), "text/html")));
            //

            // create the Multipart and its parts to it
            MimeMultipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            mp.addBodyPart(mbp2);
            mp.setSubType("alternative");

            // add the Multipart to the message
            msg.setContent(mp);

            // send the message
            Transport.send(msg);
        }
    }
}

/* This class implements a typed DataSource from :
 *      an InputStream
 *      a byte array
 *      a String
 */
class ByteArrayDataSource implements DataSource {
    private byte[] data; // data
    private String type; // content-type
   /* Create a datasource from a String */
    ByteArrayDataSource(String data, String type) {
        try {
            // Assumption that the string contains only ascii
            // characters ! Else just pass in a charset into this
            // constructor and use it in getBytes()
            this.data = data.getBytes("iso-8859-1");
        } catch (UnsupportedEncodingException uex) { }
        this.type = type;
    }

    public InputStream getInputStream() throws IOException {
        if (data == null)
            throw new IOException("no data");
        return new ByteArrayInputStream(data);
    }

    public OutputStream getOutputStream() throws IOException {
        throw new IOException("cannot do this");
    }

    public String getContentType() {
        return type;
    }

    public String getName() {
        return "Asha";
    }
}

-----Original Message-----
From: Venugopal MD [mailto:[EMAIL PROTECTED]]
Sent: Friday, November 26, 1999 9:23 PM
To: [EMAIL PROTECTED]
Subject: Need Help


Hi All ,
Can any one get me the source by which i could send an
automatic mail if anyone visits my page and
fill the form and submit .
So that  the submitted details are mailed to me and also stored
in a database .
I would like to use servlets for this .
Any sites where i get example for this ??

Expecting good response.
Regards,
Venugopal MD

___________________________________________________________________________
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