I want to trigger my email via servlets, but it is not triggering. When I run code as a standalone Java application, it is working fine. Below is my code.
Servlet is giving me problem, from server I am not able to trigger the code
package model;
import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
importjavax.naming.NamingException;
import com.sun.xml.internal.bind.CycleRecoverable.Context;
public class TestJavaMail
{
String userid ;
String password;
public TestJavaMail(String userid , String password)
{
this.userid = userid;
this.password =password;
}
public void process()
{
String[] to = {"faiz.akhtar@agnitio-technologies.com","manish.kaushik@agnitio- technologies.com",
"sandeep.sharma@agnitio-technologies.com"};
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.exchangemails.com");
props.put("mail.smtp.port", 25);
props.put("mail.smtp.user", userid);
props.put("mail.smtp.pass", password);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth ", "true");
Authenticator auth = new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userid, password);
}
};
Session session = Session.getDefaultInstance(props,auth);
try
{
MimeMessage msg = new MimeMessage(session);
System.out.println("Mimemessage ceated");
InternetAddress[] iaFrom = { new InternetAddress(
"moonis.raza@agnitio-technologies.com") };
msg.setFrom(iaFrom[0]);
InternetAddress[] iaTo = new InternetAddress[to.length] ;
for(int i=0;i<to.length;i++)
{
iaTo[i] = new InternetAddress(to[i]);
msg.addRecipient(Message.RecipientType.TO, iaTo[i]);
}
msg.setSubject("Test Java Mail");
msg.setSentDate(new Date());
msg.setText("Hello, Congrats - It is working\n pls send acknowledgement mail to senderof u get this" +
"\n as it is part of project");
Transport tran = session.getTransport("smtp");
System.out.println("Transport object created......");
//tran.setStarttls(true);
tran.connect("smtp.exchangemails.com", 25, "moonis.raza@agnitio- technologies.com", "welcome");
//tran.connect();
msg.saveChanges();
System.out.println("Connect succeeded");
tran.sendMessage(msg, msg.getAllRecipients());
tran.close();
System.out.println("Mail Sent Successfully");
}
catch (MessagingException mex)
{
System.out.println("send failed, exception: " + mex);
}
}
}
Here is servlet code:
package com.controller;
import java.io.*;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.TestJavaMail;
/**
* Servlet implementation class emailservice
*/
public class emailservice extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
System.out.println("here");
PrintWriter writer = res.getWriter();
writer.println("HELLO WORLD");
String h = "hello";
writer.println("" +h);
writer.println("welcome ");
writer.println(req.getRemoteHost());
String userid = getServletConfig().getInitParameter("userid");
String password = getServletConfig().getInitParameter("password");
writer.println(userid);
TestJavaMail t1 = new TestJavaMail(userid, password);
t1.process();
writer.println("\n bhej di ");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
I tried to run you code for myself to see what would happen. First, I made sure I had
mail.jarfile available on the classpath. Second, I did a bit of necessary changes in order to be able to run the code without getting authentication errors. i.e, instead of:I used:
and obviously, I had to provide my own username and password for this different host.
You are right; The application worked well when was executed standalone. However, when I moved it to a
JBoss AS 7.0.0, I encountered two different problems.Initially, I faced this error:
which after some googling and finding the solution given here, I could get rid of this error. i.e, I replaced this line:
with:
Then, I tried running the application again. This time, I received the below error:
I Googled the above error and found there was a bug with my used JBoss version. So, I moved the application to a later
JBossversion which wasJBoss AS 7.1.1and I succeeded. The servlet successfully sent an email to the destination I had provided for it.I hope this investigation will be helpful to you and to any visitor who is struggling with a similar problem.