有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java传输。发送(消息)抛出NoSochProviderException

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

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) 个答案

  1. # 1 楼答案

    因为您是在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是个好主意