邮件无法使用javax.mail-api-1.5.1

时间:2015-10-31 10:42:55

标签: java javamail

我已经实施了一个相同的程序来测试我的邮件服务器。

public void sendMail() {
        System.out.println("Send Mail Called");
        try{
            System.out.println("Constructor called ");
            Properties props = new Properties();
            props.put("mail.smtp.host","host");
            props.put("mail.smtp.port", "port");    
            props.put("mail.debug", "true");
            props.put("mail.smtp.auth", "true");
            Session session = Session.getInstance(props, null);

            MimeMessage mail = new MimeMessage(session);

            //Set Mailer string in Header.. 
            mail.setHeader("X-Mailer", "ABC");
            InternetAddress[] recList = new InternetAddress[1];
            recList[0] = new InternetAddress("receient@abc.com");
            mail.setRecipients(Message.RecipientType.TO, recList);

            mail.setFrom(new InternetAddress("receiver@abc.com"));
            mail.setSubject("This is demo test mail");

            Multipart mp = new MimeMultipart();

            MimeBodyPart mbp1 = new MimeBodyPart();

            //messageMIME can be "text/plain" or "text/html" or anything related to mime
            mbp1.setContent("Hi this is test demo","text/html");
            mp.addBodyPart(mbp1);

            mail.setContent(mp);
            mail.saveChanges();

            mail.setSentDate(new Date());

            // Send the message
            Transport trans = session.getTransport("smtp");
            trans.connect("host","username","password");
            trans.sendMessage(mail, mail.getAllRecipients());
            trans.close();
            System.out.println("Mail successfully send");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

但是当我运行上面的方法时,我收到以下异常。

java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
        at javax.mail.Session.initLogger(Session.java:221)
        at javax.mail.Session.<init>(Session.java:206)
        at javax.mail.Session.getInstance(Session.java:242)
        at Register.sendMail(Register.java:125)
        at Register.doPost(Register.java:22)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)

在分析了同样的错误之后,我才知道它需要添加Maven POM以下

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.2</version>
</dependency>

但是我无法在哪里添加它,因为我没有使用Maven,我只是运行示例java程序。

1 个答案:

答案 0 :(得分:0)

您可以转到The Central Repository,查找groupId com.sun.mail,选择最新版本的artefactId javax.mail,最后下载javax.mail-1.5.4.jar并将其添加到您的项目或类路径中, jar包含MailLogger类。