Transport.send(消息)抛出NoSuchProviderException

时间:2014-01-27 05:08:14

标签: java eclipse

我正在Windows下的Eclipse Kepler中创建一个动态Web项目,它将向某个地址发送电子邮件。我添加了“ activation.jar ”&我的lib文件夹中的“ javax.mail-1.5.1.jar ”。问题是,我第一次运行它时,工作正常。但是在试图再次休息几天后,它显示出以下异常

javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:800)
at javax.mail.Session.getTransport(Session.java:736)
at javax.mail.Session.getTransport(Session.java:676)
at javax.mail.Session.getTransport(Session.java:656)
at javax.mail.Session.getTransport(Session.java:713)
at javax.mail.Transport.send0(Transport.java:248)
at javax.mail.Transport.send(Transport.java:124)
at com.uks.pms.common.mail.dao.MailDAO.sendEmail(MailDAO.java:183)
at com.uks.pms.user.attendance.action.ATUpdateAction.update(ATUpdateAction.java:188)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
...................................................

我为执行此操作而编写的方法如下 -

public void sendEmail(MailBean mailConfig, String fromID, String toID,
        String ccTo, StringBuffer mailContent) throws AddressException,
        SendFailedException, MessagingException, Exception {

    javax.mail.Session session = null;
    MimeMessage mimeMessage = null;

    try {
        session = getSession(mailConfig);
        mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress(fromID));
        mimeMessage.addRecipient(Message.RecipientType.TO,
                new InternetAddress(toID));
        mimeMessage.setSubject(MailUtil.MAIL_CONTENT_SUBJECT);
        if (!(ccTo == null || ccTo.isEmpty())) {
            mimeMessage.addRecipient(Message.RecipientType.CC,
                    new InternetAddress(ccTo));
        }
        mimeMessage.setText(mailContent.toString());
        mimeMessage.setContent(mailContent.toString(),
                MailUtil.MAIL_CONTENT_TYPE);
        Transport.send(mimeMessage);
        System.out
                .println("IF YOU ARE SEEING THIS MESSAGE THEN IT MEANS YOU HAVE SUCCESSFULLY SENT THE EMAIL");
    } finally {
        System.gc();
    }
}

private javax.mail.Session getSession(MailBean mailConfig) throws Exception {
    Properties properties = null;
    javax.mail.Session session = null;

    try {
        properties = System.getProperties();
        properties.setProperty(MailUtil.KEY_MAIL_SMTP_PORT,
                mailConfig.getPortNo());
        properties.setProperty(MailUtil.KEY_MAIL_SMTP_HOST,
                mailConfig.getMailServer());

        session = javax.mail.Session.getDefaultInstance(properties);
    } finally {
        System.gc();
    }
    return session;
}

其中端口号为587 &网络中的主机为172.51.10.40 。我使用hmailserver作为邮件服务器,没有任何身份验证和&所有防火墙设置都已正确设置。我无法理解问题发生在哪里。请给出一些解决方案。如果需要任何其他信息,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:0)

因为您在servlet容器中运行它,所以可能会遇到Bug 6668 -skip unusable Store and Transport classes。如果可以,请升级到JavaMail 1.5.3。否则,将会话创建更改为以下内容:

    final ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(Session.class.getClassLoader());
    try {
        session = javax.mail.Session.getDefaultInstance(properties);
    } finally {
        Thread.currentThread().setContextClassLoader(ccl);
    }

您的代码中fix the common mistakes是个好主意。