Java Jar文件从批处理文件运行时不发送电子邮件?

时间:2013-12-09 12:31:03

标签: java batch-file

所有

以下代码工作正常,并在eclipse中运行时发送电子邮件。但是,当我从eclipse作为jar文件导出并从批处理文件运行以下命令时,它给了我错误。注意:从批处理文件中运行时,jar文件中的其他文件可以正常工作。

java -cp C:\Users\Administrator\Email.jar LotusNotes.SendEmail

例外:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/MessagingE
xception
        at LotusNotes.RunBackUpSourceCopy.main(RunBackUpSourceCopy.java:49)
        Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

SendEmail类:

public class SendEmail {

public  String to ;
public  String from;
public  String host;

public SendEmail(){
       to = "xyz@gmail.com";
       from = "localhost";
       host = "localhost";}



public  void send_production_email(String reportDate){

    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);

      Session session = Session.getDefaultInstance(properties);

      try{

       MimeMessage message = new MimeMessage(session);
       message.setFrom(new InternetAddress(from));
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
       message.setSubject("Subject Line!");
       message.setText("Test email!");
       Transport.send(message);
       System.out.println("Sent message successfully....");
      }
      catch (MessagingException mex) {
         mex.printStackTrace();
      }

}


public static void main(String[] args) {

        System.out.println("Preparing to Send Email ...");

        SendEmail email = new SendEmail();
        email.send_production_email("test production");

}

}

1 个答案:

答案 0 :(得分:2)

您需要在类路径中添加javax mail jar

修改

java -cp "C:\Users\Administrator\Email.jar";"C:\Users\Administrator\ExternalJarFiles\mail.jar"; LotusNotes.SendEmail

根据以下评论......