protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
HttpSession session = request.getSession();
ServletContext context = getServletContext();
String username =(String)session.getAttribute("username");
String rights = (String)session.getAttribute("rights");
String password = (String)session.getAttribute("password");
String userMail = (String)session.getAttribute("mail");
String recipients[] = new String[1];
recipients[0]= userMail;
String from =(String)session.getAttribute("AdministratorMail");
String message="Your user name : "+username+"\n Your password : "+password+"\n Your rights : "+rights;
String subject="Account details";
try
{
postMail(recipients,subject ,message, from);
context.getRequestDispatcher("/CreateAccountPage.jsp").forward(request, response);
}
catch (MessagingException ex)
{
Logger.getLogger(SendEmailServlet.class.getName()).log(Level.SEVERE, null, ex);
}
} finally {
out.close();
}
}
public void postMail(String recipients[],String subject,String message,String from)throws MessagingException
{
boolean debug = false;
Properties prop = new Properties();
prop.put("mail.smtp.host", "wabe.csir.co.za");
Session sessions = Session.getDefaultInstance(prop);
sessions.setDebug(debug);
Message msg = new MimeMessage(sessions);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress addressTo[] = new InternetAddress[recipients.length];
for(int x=0;x<addressTo.length;x++)
{
addressTo[x] = new InternetAddress(recipients[x]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(message,"text/plain");
Transport.send(msg);
}
code for java main class
public class JavaApplication1 {
public void postMail(String recipients[],String subject,String message,String from)throws MessagingException
{
boolean debug=false;
Properties props = new Properties();
props.put("mail.smtp.host", "wabe.csir.co.za");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress [] addresTo = new InternetAddress[recipients.length];
for(int i =0;i<recipients.length;i++)
{
addresTo[i]= new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addresTo);
msg.setSubject(subject);
msg.setContent(message,"text/plain");
Transport.send(msg);
}
public static void main(String[] args)
{
String recipients[] = new String[1];
recipients[0]="marhubane@gmail.com";
String username ="Vusi";
String rights = "None";
String password = "testf6876";
String subject="Account details";
String message="Your user name : "+username+"\n Your password : "+password+"\n Your rights : "+rights;
String from ="VMasanabo@csir.co.za";
JavaApplication1 application1 = new JavaApplication1();
try {
application1.postMail(recipients, subject, message, from);
} catch (MessagingException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}