views:

39

answers:

2

Hello All,

I am having a textbox in my jsp and would like to send an e-mail to the receipent which his/her e-mail is entered in the textbox.

Can you please guide me on how to do that.

I have just checked out this code:

<html>
<head>
    <title>mailto Example</title>
</head>

<body>
<form action="mailto:[email protected]" method="post" enctype="text/plain" >
FirstName:<input type="text" name="FirstName">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
A: 

The common way to do this is to have some server side script, e.g. in php, which will take the form's values, create an email from that and send it.

The form data could of course be sent via javascript / ajax, but that I think would not be necessary when using a php script.

Martin
+1  A: 

You need to post your form to a servlet and from servlet execute this method to send mail. your form should be

<form action="sendMail.do" method="post" enctype="text/plain" >
FirstName:<input type="text" name="FirstName">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="Submit">
</form>

Here is code to send email from java,do proper mapping for servlet in web.xml
For servlet tutorials check it here

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "smtp.jcom.net");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
org.life.java
Can I do that in the same jsp. Just for testing
maas
You can, I won't recommend it , better put it on Servlet ,
org.life.java