我使用的是 Ubuntu 11.10,jdk 1.7。我创建了一个桌面应用程序,它通过检查数据库来发送邮件。在 netbeans 7.2.1 中,应用程序在 IDE 中运行良好,但出现引导警告。但是双击时 jar 构建没有响应。当通过终端中的命令行执行 jar 文件时,它显示 No Class Found Exception javax.mail。 Authenticator.Is 设置类路径是我的问题吗?用于发送邮件的代码。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Desk_mail
{
private String from = "mailid";
private String subject = "Test Mail";
private String smtpServ = "smtp.gmail.com";
DBConnection dbcon = new DBConnection();
Desk_mailPojo dmp ;
public static void main(String[] args) throws Exception
{
try{
int status;
Desk_mail m1=new Desk_mail();
status=m1.changestatus();
if(status==1)
{
System.out.println("Email sent succesfull");
}
}
catch(Exception e){
try{
Desk_mail m1=new Desk_mail();
String p=e.getMessage()+e.getStackTrace();
m1.sendmail(p, "mailid");
}
catch(Exception en){
throw new Exception("changestatus" + en.getMessage());
}
throw new Exception("main" + e.getMessage());
}
}
public int changestatus() throws Exception
{
String status,email,body;
String policyname,policyno;
int remdays;
int detailsid;
Date remdate ;
Date enddate;
Date expdate;
readxml rl= new readxml();
dbcon.results = new ArrayList();
try{
Desk_mail m1=new Desk_mail();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
System.out.println(dateFormat.format(date));
dbcon.query="-----MYSQL QUERY----- ";
dbcon.pst = dbcon.getConnection().prepareStatement(dbcon.query);
dbcon.rs = dbcon.pst.executeQuery();
while(dbcon.rs.next()) {
dbcon.results.add(new Desk_mailPojo(dbcon.rs.getInt(1), dbcon.rs.getString(2),dbcon.rs.getString(3),dbcon.rs.getDate(4), dbcon.rs.getString(5), dbcon.rs.getString(6), dbcon.rs.getString(7)));
}
Iterator iter = dbcon.results.iterator();
while (iter.hasNext())
{
Desk_mailPojo dmp= (Desk_mailPojo)iter.next();
enddate=dmp.getDate();
email=dmp.getEmail();
policyname=dmp.getPolicyname();
policyno=dmp.getPolicyno();
detailsid = dmp.getDetailsid();
remdays=Integer.parseInt(dmp.getRemindme());
Calendar c = Calendar.getInstance();
c.setTime(enddate);
c.add(Calendar.DAY_OF_MONTH, -remdays);
String output = dateFormat.format(c.getTime());
remdate= dateFormat.parse(output);
c.setTime(enddate);
c.add(Calendar.DATE, 5);
expdate = dateFormat.parse(dateFormat.format(c.getTime()));
if(date.before(remdate)){
dbcon.query="-----MYSQL QUERY-----" ;
dbcon.pst = dbcon.getConnection().prepareStatement(dbcon.query);
dbcon.pst.setInt(1, detailsid);
dbcon.result=dbcon.pst.executeUpdate();
}
else if (((date.after(remdate))||(date.equals(remdate)))&&(date.before(enddate))){
dbcon.query="-----MYSQL QUERY-----" ;
dbcon.pst = dbcon.getConnection().prepareStatement(dbcon.query);
dbcon.pst.setInt(1, detailsid);
dbcon.result=dbcon.pst.executeUpdate();
if (dbcon.result>0){
body="The insurance policy "+policyname+"with policyno:"+policyno+" will be expired on "+ enddate;
m1.sendmail(body, email);
}
}
else if((date.after(remdate))&&(date.after(enddate))){
dbcon.query="-----MYSQL QUERY----- " ;
dbcon.pst = dbcon.getConnection().prepareStatement(dbcon.query);
dbcon.pst.setInt(1, detailsid);
dbcon.result=dbcon.pst.executeUpdate();
if ((dbcon.result>0)&&(date.before(expdate))){
body="The insurance policy "+policyname+"with policyno:"+policyno+" has been expired on "+ enddate;
m1.sendmail(body, email);
}
}
}
}catch(Exception e){
throw new Exception("changestatus" + e.getMessage());
}
return dbcon.result;
}
public int sendmail(String message,String email) throws Exception
{
try{
Properties props =System.getProperties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host",smtpServ);
props.put("mail.smtp.auth","true");
Authenticator auth = new Desk_mail.SMTPAuthenticator();
Session session=Session.getInstance(props,auth);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
msg.setSubject(subject);
msg.setText(message);
msg.setHeader("MyMail", "Mr. XYZ" );
msg.setSentDate(new Date());
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(message);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
msg.setContent(multipart);
Transport.send(msg);
System.out.println("Message sent to"+email+" OK." );
return 0;
}
catch(Exception e)
{
return -1;
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
@Override
public PasswordAuthentication getPasswordAuthentication()
{
String username = "mailid"; // specify your email id here (sender's email id)
String password = "*****"; // specify your password here
return new PasswordAuthentication(username, password);
}
}
}
我也在阅读 xml 。该类的代码如下。
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class readxml {
public static void main(String argv[]) throws Exception {
try {
String path=System.getProperty("user.dir");
path=path+"/dbconfig.xml";
File fXmlFile = new File(path);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("db");
// String username
System.out.println("-----------------------");
String username;
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// url = getTagValue("username", eElement);
// driver = getTagValue("username", eElement);
// username = getTagValue("username", eElement);
// password = getTagValue("username", eElement);
//
System.out.println("URL" + getTagValue("url", eElement));
System.out.println("driver" + getTagValue("driver", eElement));
System.out.println("username" + getTagValue("username", eElement));
System.out.println("password " + getTagValue("password", eElement));
}
}
} catch (Exception e) {
try{
throw new Exception("" + en.getMessage());
}
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
这是我尝试使用以下命令执行 jar 文件时命令行的响应:java -jar Myjarfile.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
at java.lang.Class.getMethod0(Class.java:2694)
at java.lang.Class.getMethod(Class.java:1622)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 6 more