aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "exception in  Servlet Mail" Watch "exception in  Servlet Mail" New topic
Author

exception in Servlet Mail

Karthik Soundararajan
Greenhorn

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:
--------------
mail.store.protocol=imap
mail.user=userid
mail.transport.protocol=smtp
mail.pop3.user=pop3userid
mail.debug=true
mail.from=custserv@mycompany.com
mail.host=mail.mycompany.com
mail.imap.host=mail.mycompany.com
mail.pop3.host=pop3.mycompany.com

Program is
----------

import java.io.*;

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();

res.setContentType("text/html");
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>");
pw.println("<TR><TD>Enclosure:</TD><TD>"
+ "<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("</TABLE></FORM>");
pw.println("<STRONG>" + successMessage + "</STRONG>");
pw.println("</BODY></HTML>");
}

/**
* 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"));

sendMessage();
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("mail.host", "mail.mycompany.com");
mailProps.put("mail.user", "userid");
mailProps.put("mail.from", "userid@mycompany.com");
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
mailAuth.setUser("userid");
mailAuth.setPassword("password");

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
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));
msg.setSubject(subject);
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 {
mbp1.setText(messageText);
}

mp.addBodyPart(mbp1);

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

mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
}

msg.setContent(mp);
msg.saveChanges();

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

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

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: mail.mycompany.com, port: 25; nested exception is: java.net.SocketException: Software caused connection abort: connect
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Karthik Soundararajan:
Could not connect to SMTP host: mail.mycompany.com, port: 25; nested exception is: java.net.SocketException: 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
hi,

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

open a command prompt
telnet mail.mycompany.com 25

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

regards..


SCJP/SCWCD
Ragav Seth
Greenhorn

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.
 
Don't get me started about those stupid light bulbs.
 
subject: exception in Servlet Mail