请帮忙, javax 邮件项目在从 netbeans 运行时发送电子邮件,但是当直接从可执行 jar 文件运行时,邮件不会发送。并且也无法在任何其他 PC 和服务器上运行。
注意:- JOptionPane 显示消息发送成功。但收件人没有收到任何邮件。
public static void sendMail(String recepient) throws Exception{
System.out.println("Preaparing to send Email");
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "false");
properties.put("mail.smtp.host", "mail.xyz.in");
properties.put("mail.smtp.port", "587");
final String myAccountEmail = Sender;
final String password = email_pass;
Session session = Session.getInstance(properties, new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(myAccountEmail, password);
}
});
Message message = prepareMessage(session,myAccountEmail,recepient);
Transport.send(message);
System.out.println("Message Send Successful");
JOptionPane.showMessageDialog(null, "Successful");
}
private static Message prepareMessage(Session session,String myAccountEmail, String recepient) {
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(myAccountEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
message.setSubject(sub);
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(msg);
Multipart multipartObject = new MimeMultipart();
multipartObject.addBodyPart(messageBodyPart1);
message.setContent(multipartObject);
return message;
}
catch(Exception e){JOptionPane.showMessageDialog(null, e.toString());
e.printStackTrace();
}
return null;
}
我的猜测是,可执行 jar 文件需要 main 函数的定义作为入口点。我可以想象 netbeans 在幕后自动执行此操作,以更快地调试您的代码。
public class Main {
// your function definitions here
public static void main(String[] args){
// Your function calls here
}
}