<HTML> <BODY> <FORM action="sendmail.jsp" method="post"> <TABLE align="center"> <TR> <TD width="50%"> To:<BR><INPUT name="to" size="25"> </TD> <TD width="50%"> From:<BR><INPUT name="from" size="25"> </TD> </TR> <TR> <TD colspan="2"> Subject:<BR><INPUT name="subject" size="50"> </TD> </TR> <TR> <TD colspan="2"> Message:<BR><TEXTAREA name="text" rows=25 cols=85></TEXTAREA> </TD> </TR> </TABLE> <INPUT type="submit" name="cb_submit" value=" Send "> <INPUT type="reset" name="cb_reset" value=" Clear "> </FORM> </BODY> </HTML> |
Properties props = new Properties (); props.put("mail.smtp.host", "smtp.jspinsider.com"); |
Session sendMailSession; sendMailSession = Session.getInstance(props, null); |
Transport transport; transport = sendMailSession.getTransport("smtp"); |
Message newMessage = new MimeMessage(sendMailSession); |
<%@ page import= " javax.mail.*, javax.mail.inte.net.*, javax.activation.*, java.util.*" %> |
newMessage.setFrom(new InternetAddress(request.getParameter("from"))); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to"))); newMessage.setSubject(request.getParameter("subject")); newMessage.setSentDate(new Date()); newMessage.setText(request.getParameter("text")); |
transport.send(newMessage); |
Sample JSP email Utility Using JavaMail <%@ page import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*" %> <html> <head> <TITLE>JSP meets JavaMail, what a sweet combo.</TITLE> </HEAD> <BODY> <% try{ Properties props = new Properties(); Session sendMailSession; Store store; Transport transport; sendMailSession = Session.getInstance(props, null); props.put("mail.smtp.host", "smtp.jspinsider.com"); Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(request.getParameter("from"))); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to"))); newMessage.setSubject(request.getParameter("subject")); newMessage.setSentDate(new Date()); newMessage.setText(request.getParameter("text")); transport = sendMailSession.getTransport("smtp"); transport.send(newMessage); %> <P>Your mail has been sent.</P> <% } catch(MessagingException m) { out.println(m.toString()); } %> </BODY> </HTML> |