Я запустил эту программу в Eclipse, и она работает. Но при компиляции с использованием командной строки, используя javac mail.java или java -classpath. myClass, выдает ошибку.
Это код.
public class mail {
private static String USER_NAME = "***"; // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "****"; // GMail password
private static String RECIPIENT = "****";
private static String RECIPIENT_A="****";
public static void main(String[] args) {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = { RECIPIENT,RECIPIENT_A }; // list of recipient email addresses
String subject = "MailCheck";
String body = "JAVAMAIL check";
sendFromGMail(from, pass, to, subject, body);
}
private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
System.out.println("props setting over");
Session session = Session.getInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i = 0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
System.out.println("array settings over");
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Mail Sending Over");
transport.close();
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
}
Библиотеки Javax.mail находятся в папке lib в том же проекте. Эти файлы .jar не обнаруживаются при компиляции с использованием командной строки.
C:\Workspace\MailCheck\mailCheck\src\com\atos>javac mail.java
mail.java:6: package javax.mail does not exist
import javax.mail.*;
^
mail.java:7: package javax.mail.internet does not exist
import javax.mail.internet.*;
^
mail.java:39: cannot find symbol
symbol : class Session
location: class com.atos.mail
Session session = Session.getInstance(props);
^
mail.java:39: cannot find symbol
symbol : variable Session
location: class com.atos.mail
Session session = Session.getInstance(props);
^
mail.java:40: cannot find symbol
symbol : class MimeMessage
location: class com.atos.mail
MimeMessage message = new MimeMessage(session);
^
mail.java:40: cannot find symbol
symbol : class MimeMessage
location: class com.atos.mail
MimeMessage message = new MimeMessage(session);
^
mail.java:43: cannot find symbol
symbol : class InternetAddress
location: class com.atos.mail
message.setFrom(new InternetAddress(from));
Также все внешние файлы JAR правильно включены в classpath. Любые решения?