This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Servlets and the fly likes exception in  Servlet Mail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "exception in  Servlet Mail" Watch "exception in  Servlet Mail" New topic

exception in Servlet Mail

Karthik Soundararajan

Joined: Jan 03, 2006
Posts: 20
Hello experts i am trying a simple mail program in servlets using Weblogic server myself configured th weblogic server like this
JNDI Name is:BibleMailSession

Properties are:

Program is


import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

import javax.activation.*;

import javax.mail.*;
import javax.mail.internet.*;

import javax.naming.*;

* Class SendMailServlet2
* @author
* @version %I%, %G%
public class SendMailServlet2 extends HttpServlet {

private String to = "";
private String cc = "";
private String bcc = "";
private String subject = "";
private String filename = "";
private String messageText = "";
private boolean isHTML;
private String successMessage = "";

* Method doGet
* @param req
* @param res
* @throws IOException
* @throws ServletException
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

PrintWriter pw = res.getWriter();

pw.println("<HTML><HEAD><TITLE>Send Mail - Complex</TITLE></HEAD>");
pw.println("<BODY><H1>Send Mail - Complex</H1>");
pw.println("<FORM ACTION='sendmail2' METHOD=post><TABLE>");
pw.println("<TR><TD>To:</TD><TD><INPUT NAME=to size=60></TD></TR>");
pw.println("<TR><TD>Cc:</TD><TD><INPUT NAME=cc size=60></TD></TR>");
pw.println("<TR><TD>Bcc:</TD><TD><INPUT NAME=bcc size=60></TD></TR>");
pw.println("<TR><TD>Subject:</TD><TD><INPUT NAME=subject size=60>"
+ "</TD></TR>");
+ "<INPUT NAME=filename size=60></TD></TR>");
pw.println("<TR><TD VALIGN>Message:</TD>");
pw.println("<TD><TEXTAREA NAME=messageText rows=10 cols=60>"
+ "</TEXTAREA></TD></TR>");
pw.println("<TR><TD>HTML:</TD><TD><input type=checkbox NAME=isHTML>"
+ "</TD></TR>");
pw.println("<TR><TD><INPUT TYPE=SUBMIT NAME=Submit VALUE=Submit>"
+ "</TD></TR>");
pw.println("<STRONG>" + successMessage + "</STRONG>");

* Method doPost
* @param req
* @param res
* @throws IOException
* @throws ServletException
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

to = req.getParameter("to");
cc = req.getParameter("cc");
bcc = req.getParameter("bcc");
subject = req.getParameter("subject");
filename = req.getParameter("filename");
messageText = req.getParameter("messageText");
isHTML = "on".equals(req.getParameter("isHTML"));

doGet(req, res);

* Method sendMessage
public void sendMessage() {

try {

// lookup mail session in JNDI
InitialContext context = new InitialContext();
Session session = (Session) context.lookup("BibleMailSession");

// override mail session properties
Properties mailProps = new Properties();

mailProps.put("mail.transport.protocol", "smtp");
mailProps.put("", "");
mailProps.put("mail.user", "userid");
mailProps.put("mail.from", "");
mailProps.put("mail.debug", "true");
mailProps.put("mail.smtp.auth", "true");

// get an authenticated session
MailAuthenticator mailAuth = new MailAuthenticator();

// For testing, set the values here. The user should be prompted or
// retrieved from a database or descriptor

Session authSession = session.getInstance(mailProps, mailAuth);

// set message with to, subject, and message text
Message msg = new MimeMessage(authSession);

// set message with to, cc, bcc, and subject
InternetAddress.parse(to, false));
InternetAddress.parse(cc, false));
InternetAddress.parse(bcc, false));
msg.setSentDate(new Date());

// set with message text via a mime multipart
Multipart mp = new MimeMultipart();
MimeBodyPart mbp1 = new MimeBodyPart();

if (isHTML) {
mbp1.setContent(messageText, "text/html");
} else {


if (!filename.equals("")) {
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);

mbp2.setDataHandler(new DataHandler(fds));


// get transport and send message
Transport authTransport = authSession.getTransport();

authTransport.sendMessage(msg, msg.getAllRecipients());

successMessage = "Message sucessfully sent.";
session = null;
} catch (Exception e) {
successMessage = e.getMessage();
while executing this program i am getting an exception i tried a lot but i am unable to solve it myself listed the exception below please help me to overcome this

Exception is

Could not connect to SMTP host:, port: 25; nested exception is: Software caused connection abort: connect
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Karthik Soundararajan:
Could not connect to SMTP host:, port: 25; nested exception is: Software caused connection abort: connect

I'm not sure we need all of the imformation for the error displayed. Can you check toi make sure you're using the latest version of the JavaMail API, I've had strange problems that have been solved by updating to the latest version.
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78

did you try to conenct to the mail server directly?I mean:

open a command prompt
telnet 25

if no error messages, and black screen then connection is OK.


Ragav Seth

Joined: Jan 17, 2005
Posts: 16
I have got this problem before.Check your system to see if you have an underlying virus scanner running. If so then this will cause that error called SMTP cannot connect to port 25.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: exception in Servlet Mail
It's not a secret anymore!