我有一个可以发送电子邮件的程序。但是,这需要成为更大规模计划的一部分。 Email类在不同的包下,而我的其他2个类(驱动程序类/主程序,以及另一个对象类)都在默认包中。我是否可以访问电子邮件类,尽管它位于不同的包中,或者我是否需要将它们全部放在一个包中?我该怎么做呢?目前,我尝试删除电子邮件类的主要方法部分并将其放入带有我的驱动程序类的默认包中,这导致了许多语法错误。下面是一些显示我的课程和一些代码的照片。 SendMail类与SendMailTLS相同,只是删除了main方法并将其放入默认包中。 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 = "treybyroncollier@gmail.com";
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("treybyroncollier@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("treycollier@live.co.uk"));
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 = "treybyroncollier@gmail.com";
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("treybyroncollier@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("treycollier@live.co.uk"));
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);
}
}
}
答案 0 :(得分:0)
您应首先学习基础知识,然后从这样一个复杂的程序开始。
在您的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 {
public static void start() {
final String username = "treybyroncollier@gmail.com";
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("treybyroncollier@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("treycollier@live.co.uk"));
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);
}
}
}
然后,您可以从新的main方法或任何其他您喜欢的方法运行该代码。
public class YourMainClass {
public static void main(String[] args) {
SendMail.start();
}
}
简而言之,如果您不希望在启动程序时执行主要方法,只需将其名称从main
更改为您选择的内容,然后删除参数String[] args
。