send an e-mail to the text "e-mail" which is in the textbox
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 enter开发者_Go百科ed 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:XXX@XXX.com" 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>
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);
}
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.
精彩评论