Java Mail 项目在 netbeans 中运行时可以工作,但在直接运行 dist/executble jar 文件时不能工作

问题描述 投票:0回答:1

请帮忙, 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;
}

java email netbeans java-package javax.mail.address
1个回答
0
投票

我的猜测是,可执行 jar 文件需要 main 函数的定义作为入口点。我可以想象 netbeans 在幕后自动执行此操作,以更快地调试您的代码。

public class Main {
    // your function definitions here
    
    public static void main(String[] args){
            // Your function calls here
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.