Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8762851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:38:12+00:00 2026-06-13T15:38:12+00:00

I want to trigger my email via servlets, but it is not triggering. When

  • 0

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 {

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:38:13+00:00Added an answer on June 13, 2026 at 3:38 pm

    I tried to run you code for myself to see what would happen. First, I made sure I had mail.jar file 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:

    props.put("mail.smtp.host", "smtp.exchangemails.com"); 
    

    I used:

    props.put("mail.smtp.host", "smtp.gmail.com");
    

    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:

    at javax.mail.Session.getDefaultInstance(Session.java:320) [mail-1.4.4.jar:1.4.4]
    at test.TestJavaMail.process(TestJavaMail.java:42) [classes:]
    

    which after some googling and finding the solution given here, I could get rid of this error. i.e, I replaced this line:

    Session session = Session.getDefaultInstance(props, auth);
    

    with:

    Session session = Session.getInstance(props, auth);
    

    Then, I tried running the application again. This time, I received the below error:

    java.lang.NoClassDefFoundError: javax/net/ssl/SSLPeerUnverifiedException
        com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
        com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
        javax.mail.Service.connect(Service.java:295)
        test.TestJavaMail.process(TestJavaMail.java:63)
    

    I Googled the above error and found there was a bug with my used JBoss version. So, I moved the application to a later JBoss version which was JBoss AS 7.1.1 and 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have C# web application that I want it to send out an email
I have a trigger where I want to send out an email on updates
I want to trigger an save action anywhere in my application (Control+S). I've added
I want to create a process in my ASP.NET application that I trigger manually
I want to trigger an email if a new system admin is granted. I
I have a an application sitting on a desktop and I want to run
I want to trigger a long running background process (coded as a service) from
I want to trigger an event with a button that changes the visibility of
I want to trigger a special action in the save() method of a Django
I want to trigger on click of a button this: <asp:Button runat=server ID=submit Text=Submit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.