[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Can't send email. My java program used to be working running on server BUT does not work on servlet????????

Posted on 2005-05-05
17
Medium Priority
?
533 Views
Last Modified: 2008-01-09
I got a jave program used for sending email, it works running on server but NOT working on servlet.
I have done a JSP page which is invoked the email bean to send email

See my code below ( The Email is run on servlet)

package TEST;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;

public class Email
{
  public Email()
  {
  }
 
  public static int Send(String to
                        ,String from
                        ,String host
                        ,String emailSubject
                        ,String loginUser
                        ,String loginPwd
                        ,String msgText1
                        ,String msgText2
                        ,String AttachedFileName
                        ,String AttachmentName) throws Exception {

     Properties props = new Properties();
     props.put("mail.smtp.host", host);
     props.put("mail.smtp.auth", "true");

     emailSubject = emailSubject;
     Session session = Session.getDefaultInstance(props, null);
     //session.setDebug(debug);
     try {
       // create a message
       MimeMessage msg = new MimeMessage(session);
       InternetAddress[] address = {new InternetAddress(to)};
       msg.setFrom(new InternetAddress(from));
       msg.setRecipients(Message.RecipientType.TO, address);
       msg.setSubject(emailSubject);
       msg.setSentDate(new Date());
       msg.setText(msgText1);
       // Send email using SMTP
       Transport transport = session.getTransport("smtp");
       //transport.connect(host, loginUser, loginPwd);
       transport.connect("MAILSERVERHOST", "myEmailAccount", "myEmailPwd");
       Multipart mp = new MimeMultipart();
       if (msgText1 != null && !msgText1.equals("NULL")) {
            MimeBodyPart mbp = new MimeBodyPart();
            mbp.setText(msgText1);
            mbp.setHeader("Content-Type", "text/plain");
            mp.addBodyPart(mbp);
        }
        if (AttachedFileName != null && (!AttachedFileName.equalsIgnoreCase("NULL"))) {
            MimeBodyPart mbp = new MimeBodyPart();
            mbp.setHeader("Content-Type", "application/pdf");
            File attachments = new File(AttachedFileName);
            FileDataSource fileDataSource = new FileDataSource(attachments);
            mbp.setDataHandler(new DataHandler(fileDataSource));
            mbp.setFileName(AttachmentName);
            // mbp.setFileName(AttachedFileName);
            mp.addBodyPart(mbp);
            msg.setContent(mp);
        }

        msg.saveChanges();
        //msg.setContent(mp);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
     }
     catch (AddressException ae) {
         System.out.println("address exception Error occured due to " + ae);
         ae.printStackTrace();
         Exception ex = null;
         if ((ex = ae.getNextException()) != null) {
             ex.printStackTrace();
         }
         return 1;
     }
     catch (Exception e)
     {
       System.out.println("Send:Exception due to " + e);
       e.printStackTrace();
       return 1;
     }
     return 0;
   } // Send

}


===================
My JSP invokes Email.send
==============


<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql"%>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*, AAPL.*" errorPage="" %>
<P>

<%
    Email email = new Email();
    email.Send("Paul.smith@au.fujitsu.com"
              ,"Paul.smith@au.fujitsu.com"
              ,"SYD78CALIF"
              ,"My test"
              ,"SMITHP"
              ,"SPY"
              ,"Hope you are OK"
              ,""
              ,""
              ,"");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Employee Management</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="PRAGMA" value="NO-CACHE">
<DIV align="right">
  <img src="image/img_crm.jpg" width="360" height="70"/>
</DIV>
</P>The email has been send to YOU shortly
</head>


===================
Error messages

5/05/06 15:11:12 javax.mail.internet.ParseException
05/05/06 15:11:12       at javax.mail.internet.ParameterList.<init>(ParameterList.java)
05/05/06 15:11:12       at javax.mail.internet.ContentDisposition.<init>(ContentDisposition.java:70)
05/05/06 15:11:12       at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1040)
05/05/06 15:11:12       at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:790)
05/05/06 15:11:12       at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:216)
05/05/06 15:11:12       at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
05/05/06 15:11:12       at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:1841)
05/05/06 15:11:12       at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1819)
0
Comment
Question by:LeanMoreTryMore
  • 6
  • 5
  • 4
  • +1
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13942844
> but NOT working on servlet.

What does your servlet look like?
0
 

Author Comment

by:LeanMoreTryMore
ID: 13942856
Tomcat.

But I run this inside Oracle JDeveloper 10g
0
 
LVL 92

Expert Comment

by:objects
ID: 13942930
No I meant what does the servlet code look like that is failing.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LeanMoreTryMore
ID: 13942948
HTTP Status 500 -

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

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax/mail/internet/AddressException
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
      org.apache.jsp.GBXcustomerRegister_005faction_jsp._jspService(org.apache.jsp.GBXcustomerRegister_005faction_jsp:79)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.NoClassDefFoundError: javax/mail/internet/AddressException

0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13942976
Hi,

have you included mail.jar in your classpath?

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13942978
Hi,

I mean mail.jar from JavaMail lib.

Regards
Dave
0
 

Author Comment

by:LeanMoreTryMore
ID: 13943005
I use the mail.jar provided from Oracle. mail.jar
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13943010
Hi,

>>"it works running on server but NOT working on servlet."
Can you tell me more on it?

regards
Dave
0
 
LVL 92

Expert Comment

by:objects
ID: 13943014
Where have you put it?  Should either be in <webapp>/WEB-INF/lib or <tomcat>/common/lib
0
 
LVL 92

Expert Comment

by:objects
ID: 13943021
> it works running on server but NOT working on servlet.

That stack trace is actually from your jsp
0
 

Author Comment

by:LeanMoreTryMore
ID: 13943038
The java program used for sending email is run on our NT2003 Server. It works. NOTES. The mail.jar and activation.jar are given from SUN..NOT USE the Oracle one.


s we will developed the web application using JSP, I take this java program run on the servlet. We are using Oracle JDeveloper10g to develop JSP and Java. It runs time error when it runs.

NOTES: I COPY THE mail.jar and activation.jar from SUN to Servlet and then shut down the Apache Tomcat and Re-start again, this time I DOES NOT SHOW any error but no email is received.


Thank you in advance for both obejct and suprapto45
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 400 total points
ID: 13943050
Hi,

How about the rest *.jar from JavaMail such as smtp.jar, mailapi.jar, pop3.jar.

Have you included them?

Regards
Dave
0
 
LVL 92

Expert Comment

by:objects
ID: 13943056
> I COPY THE mail.jar and activation.jar from SUN to Servlet

Where exactly did you copy them to?
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 1200 total points
ID: 13945004
>Where exactly did you copy them to?
you are suppose to have them in web-inf/lib of your webapp..
 and as dave is saying you need
mail.jar
activation.jar
mailapi.jar
smtp.jar
imap.jar to get it to work

( all in lib directory)
0
 

Author Comment

by:LeanMoreTryMore
ID: 13956028
iT USED TO BE WORKING ON THE CLIENT SERVER where I only copied mail.jar and activation.jar

Where do i get the mailapi.jar, smtp.jar and imap.jar?

I copy to $TOMCAT_HOME/webapps/AAPL/WEB-INF/lib


0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13956186
you can download all the mail components from here

http://java.sun.com/products/javamail/index.jsp
0
 
LVL 92

Expert Comment

by:objects
ID: 13963950
> java.lang.NoClassDefFoundError: javax/mail/internet/AddressException

AddressException is in mail.jar isn't it??
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question