Java sendmail works only on development computer?

43 Views Asked by At

A sendmailcode with java programmed on Apache Netbeans only works on the development computer.

    String host = "smtp.xxxxx.com";
    String Password = "xxxxxxxxxxxxxxxxxxxx";
    String from = "[email protected]";
    String toAddress = to;
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.host", host);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.ssl.trust", "*");
    
    Session session = Session.getInstance(props, null);

    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO, toAddress);
    message.setSubject(sub);
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(mes);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(name);
    multipart.addBodyPart(messageBodyPart);
    message.setContent(multipart);

    try {
        Transport tr = session.getTransport("smtp");
        tr.connect(host, from, Password);
        tr.sendMessage(message, message.getAllRecipients());
        System.out.println("Mail Sent Successfully");
        tr.close();
    } catch (SendFailedException sfe) {
        System.out.println(sfe);
    }

If I make an installer with all files and install it on a 2nd computer sending an email will not work.

How may I find the difference between the development and other computers? Or the failure?

1

There are 1 best solutions below

0
orgen On

Found the email sent problem.

I added following property :

props.put("mail.smtp.ssl.protocols", "TLSv1.2 TLSv1.3");

Yeahhh