Write some CSS to display simple UML Class Diagram >>
<< give it a whirl
About JavaMail javax.activation.UnsupportedDataTypeException

Author Zhou Renjian Create@ 2004-11-22 23:27
whizz Note icon

Error message such as:

javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain
...

And the above error only occurs when I send my email through Servlet. For example:
=============================
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * iDeas wHizz ---#--->!
 * wHizziDeas helps whizz your ideas
 * http://www.whizzideas.com
 * Copyright whizzideas.com 2004, all rights reserved
 *
 * Created on 2004-11-14
 */

/**
 * @author Janyckee Jozz
 */
public class SendMail extends HttpServlet {
    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        SMTPClient cl=new SMTPClient();
        cl.setHost("localhost");
        String adminEmail = "jozz@whizzideas.com";
        cl.setFromAddress(adminEmail);
        if (adminEmail.equals(req.getParameter("to"))) {
            cl.setToAddresses(new String[]{ req.getParameter("to") } );
        } else {
            cl.setToAddresses(new String[]{ req.getParameter("to"), adminEmail } );
        }
        cl.setReplyToAddresses( new String[]{adminEmail} );
        cl.enableAUTH();
        cl.setUserName("jozz");
        cl.setPassword("");
        //cl.setCharset("gb2312");
        //cl.setContentType("text/plain");

        cl.setSubject(req.getParameter("subject"));
        //cl.setFileAttachments(new String[]{"attachment1.txt", "k.jqs"});
        cl.setTextContent(req.getParameter("content"));
        //cl.setContentType("text/html"); //for HTML messaging

        try {
            cl.sendMessage();
            
            PrintWriter out = resp.getWriter();
            out.println("Subject: " + req.getParameter("subject"));
            if (adminEmail.equals(req.getParameter("to"))) {
                out.println("To: " + req.getParameter("to"));
            } else {
                out.println("To: " + req.getParameter("to") + ";" + adminEmail);
            }
            out.println("Content:");
            out.println(req.getParameter("content"));
            //out.print("OK.");
        } catch (Exception e) {
            resp.getWriter().println(e.getMessage());
        }
    }
    public static void main(String[] args) {
        SMTPClient cl=new SMTPClient();
        cl.setHost("localhost");
        String adminEmail = "jozz@whizzideas.com";
        cl.setFromAddress(adminEmail);
           cl.setToAddresses(new String[]{ adminEmail } );
        cl.setReplyToAddresses( new String[]{adminEmail} );
        cl.enableAUTH();
        cl.setUserName("jozz");
        cl.setPassword("");
        cl.setCharset("gb2312");
        cl.setContentType("text/plain");

        cl.setSubject("DNS offline!");
        //cl.setFileAttachments(new String[]{"attachment1.txt", "k.jqs"});
        cl.setTextContent("Current IP: 200.2.2.11");
        //cl.setContentType("text/html"); //for HTML messaging

        cl.sendMessage();
        System.out.println("OK");
    }
}
============================

Running the above code as Java Application will send the mail, but when I tried to visit the link such as
http://localhost:8080/sendmail?to=jozz@whizzideas.com&subject=DNS+is+offline&content=Current+IP+is+220.120.3.33
to send the mail, it failed and have exception in the stderr.log.

Finally I moved activation.jar from jarkata-tomcat\commons\lib\ to the servlet's WEB-INF\lib\, and restart the tomcat, and got things go right.

It seemed that the path of activation.jar must be known by the application. Placing activation.jar in tomcat's common library folder will hide its path to the servlet, and block the servlet working right. So moving the activation.jar to WEB\lib\ will make things work.


Trackbacks[1]
Create@ 2006-08-28 21:09
whizz Note icon

Thank you very much, you help me resoving my headache problem.

I love you!

本记录所在类别:
本记录相关记录: