Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to send mail using Servlet and JSP

 
Nandan Jain
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For a small application I am using Servlet and JSP. In one servlet I am calling sendMail() funcation which should send mail to user..but here it causes problem. This sendMail() function works well when I use inside main() funcation in java file. But It starts creating problem in JSP and servlet.
following is the code inside the sendMail()function.
-------------------------------------------------------
String host = "mail.etn.com";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());
msg.setText("This is a test of sending a " +
"plain text e-mail through Java.\n" +
"Here is line 2.");
Transport.send(msg);
}catch (MessagingException mex) {
mex.printStackTrace();
}

-------------------------------------------------------

And following exception I am getting..
=======================================================
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain; charset=
us-ascii
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:885)
at javax.activation.DataHandler.writeTo(DataHandler.java:316)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
at javax.mail.Transport.send0(Transport.java:169)

... 18 more
=======================================================

I dont understand what is this ...pls help.

Regards,
Nandan.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you verified that all the jar files on your classpath are available to the web app?
If it's working in a command line app but not from a servlet, I'm guessing that you might have a different version of one of the libs (jar files) in your container.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic