我有一個程序,我可以發送電子郵件通過。但是,這需要成爲更大程序的一部分。電子郵件類是在不同的包下,而我的其他兩個類(驅動程序類/主程序,以及另一個對象類)都在默認包中。我可以訪問電子郵件類,儘管它在不同的包中,或者我是否需要將它們全部放在一個包中?我怎麼去做這些?目前,我嘗試刪除電子郵件類的主要方法部分,並將其與我的驅動程序類一起放入默認包中,這導致了很多語法錯誤。以下是一些照片,顯示我的課程和一些代碼。 SendMail類與SendMailTLS相同,只是主要方法被刪除並放入默認包中。 SendMailTLS類完美工作,我只需要能夠從IA類訪問它。我想將一個主要的方法變成一個新的非主要方法類
的SendMail類:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
final String username = "[email protected]";
final String password = "13october";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("THIS EMAIL IS A TEST");
message.setText("Hello Trey, just to let you know that this email is a test and everything is working with Java.");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
SendMailTLS類:
package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "[email protected]";
final String password = "13october";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("THIS EMAIL IS A TEST");
message.setText("Hello Trey, just to let you know that this email is a test and everything is working with Java.");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
只需將其移動到不同的包,然後再次導入新的包路徑。它看起來像你使用Eclipse,所以你應該能夠將鼠標懸停在'語法錯誤'下,然後單擊'import package.newclasspath;' – Smittey
在進入包的概念之前,我認爲你應該給出一個看看[類,方法和實例變量]的概念(https://docs.oracle.com/javase/tutorial/java/javaOO/index.html) – bracco23