Sending email from JSP

To send a Mail you can use a java.mail class. You need to add two jars into the WEB-INF/lib directory: mail.jar - contains the actual smtp implmentation and activation.jar - needed by mail.jar.

If you are using a private tomcat You need to upload them into your global lib directory or WEB-INF/lib application directory.

An example code how to use java.mail is provided below:

`<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%>

<%@page import="java.util.Properties"%>
<%@page import="javax.mail.Session"%>
<%@page import="javax.mail.Authenticator"%>
<%@page import="javax.mail.PasswordAuthentication"%>
<%@page import="javax.mail.Message"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.Transport"%>

Email Test

<%
String smtpServer = null;
String smtpPort = null;
final String authAddress = request.getParameter("auth_add");
final String authPassword = request.getParameter("auth_pass");
String subject = null;
String email = null;
String message = null;
String send = request.getParameter("send");
String siteName=request.getServerName();
siteName=siteName.replaceAll("www.","");

if(send!=null){
smtpServer = request.getParameter("smtp_server");
smtpPort = request.getParameter("smtp_port");
subject = request.getParameter("subject");
email = request.getParameter("email");
message = request.getParameter("message");
try{
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", "true");

// create some properties and get the default Session
Session sessionMail = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authAddress, authPassword);
}
});

sessionMail.setDebug(true);

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

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

InternetAddress[] addressTo = new InternetAddress[1];
addressTo[0] = new InternetAddress(email);
msg.setRecipients(Message.RecipientType.TO, addressTo);

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

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}catch(Exception e){
e.printStackTrace(response.getWriter());
}
}
%>

SMTP Server:

SMTP Port:

Username:

Password:

Subject:

Recipient:

Message Body:

 

`