我曾尝试编写使用Java发送电子邮件的代码.但是这段代码不起作用.当代码执行时,它会卡在transport.send(消息)上.它永远停留在那里.此外,我不确定其余代码是否正确.
//first from, to, subject, & text values are set
public class SendMail {
private String from;
private String to;
private String subject;
private String text;
public SendMail(String from, String to, String subject, String text){
this.from = from;
this.to = to;
this.subject = subject;
this.text = text;
}
//send method is called in the end
public void send(){
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance(props);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = null; …Run Code Online (Sandbox Code Playgroud)