【问题标题】:send mail code not work for java EE 5 web project发送邮件代码不适用于 java EE 5 Web 项目
【发布时间】:2011-06-01 17:35:18
【问题描述】:

我正在使用 myeclipse 7,我在其中添加了我需要的 java EE 5 库。如果我在应用程序外部添加 mail.jar,它会从 java EE 5 库中调用 javaee.jar。它不使用邮件.jar。如果我删除 java EE 5 库,那么它可以工作,但我需要 java EE 5 库。如何使用 java EE 5 库的 javaee.jar 发送邮件?

如果需要下面是我的 sed 邮件 hava 代码

 String  d_email = "email@gmail.com",
                d_password = "pass",
                d_host = "smtp.gmail.com",
                d_port  = "465",
                m_to = "email@gmail.com",
                m_subject = "Testing",
                m_text = "This is the testing email.";

        public Main()
        {
            Properties props = new Properties();
            props.put("mail.smtp.user", d_email);
            props.put("mail.smtp.host", d_host);
            props.put("mail.smtp.port", d_port);
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.socketFactory.port", d_port);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");

            SecurityManager security = System.getSecurityManager();

            try
            {
                Authenticator auth = new SMTPAuthenticator();
                Session session = Session.getInstance(props, auth);
                session.setDebug(true);

                MimeMessage msg = new MimeMessage(session);
                msg.setText(m_text);
                msg.setSubject(m_subject);
                msg.setFrom(new InternetAddress(d_email));
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
                Transport.send(msg);

                System.out.println("Mail Sent");
            }
            catch (Exception mex)
            {
                mex.printStackTrace();
            } 
        }

    public static void main(String[] args)
    {
        Main blah = new Main();
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator
    {
        public PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(d_email, d_password);
        }
    }

错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
    at javax.mail.Session.loadProvidersFromStream(Session.java:928)
    at javax.mail.Session.access$000(Session.java:174)
    at javax.mail.Session$1.load(Session.java:870)
    at javax.mail.Session.loadResource(Session.java:1084)
    at javax.mail.Session.loadProviders(Session.java:889)
    at javax.mail.Session.<init>(Session.java:210)
    at javax.mail.Session.getInstance(Session.java:232)
    at com.ctn.origin.connection.Main.<init>(Main.java:35)
    at com.ctn.origin.connection.Main.main(Main.java:55)

【问题讨论】:

  • 在什么情况下不起作用?它是否给你编译错误、运行时异常、没有产生预期的结果?
  • 它给出了运行时错误,我修改了描述错误的问题

标签: java email java-ee-5


【解决方案1】:

您应该手动将容器特定的库(例如 javaee.jar)添加到您的项目中。如果您在不同品牌/版本的容器上部署和运行项目,只会导致运行时问题。 javaee.jar 特定于 Glassfish。由于 Glassfish 本身已经附带了 mail.jar,这表明您的目标运行时根本不是 Glassfish。可能是 Tomcat 或 JBoss 什么的。

如果您这样做是为了克服项目编译错误,那么您应该以不同的方式解决这个问题。我不做 MyEclipse,但我相信这与 Eclipse 非常相似,所以这个基于 Eclipse 的答案也应该适用于 MyEclipse:右键单击项目,选择 Properties,转到 Target Runtime 部分并从列表中选择目标运行时(您要使用的 servlet 容器)。这样 Eclipse 将自动以正确的方式将其库包含在项目的构建路径中。

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 2011-06-16
    • 1970-01-01
    • 2021-08-04
    • 2011-11-28
    • 1970-01-01
    • 2018-01-20
    相关资源
    最近更新 更多