JavaMail - 为什么它在我的笔记本电脑上运行而不在另一台计算机或服务器上运行?

时间:2016-04-28 17:22:18

标签: java javamail

我有以下方法来拍摄电子邮件:

protected void sendEmail(SmtpConnection smtpConnection, String recipients, String subject, String htmlBody, String filename, byte[] attachment) {

    final String user = smtpConnection.getUser();
    final String password = smtpConnection.getPassword();

    System.out.println(smtpConnection.getHost());
    System.out.println(smtpConnection.getPort());
    System.out.println(user);
    System.out.println(password);

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", smtpConnection.getHost());
    props.put("mail.smtp.port", smtpConnection.getPort());

    Session session = Session.getInstance(properties, 
            new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password);
        }
    });

    List<String> recipientList = Arrays.asList(recipients.split(","));

    InternetAddress[] iAddresses = new InternetAddress[recipientList.size()];

    for (String recipient : recipientList) {
        System.out.println("email sent to: " + recipient);
        try {
            iAddresses[recipientList.indexOf(recipient)] = new InternetAddress(recipient);
        } catch (AddressException e) {
            e.printStackTrace();
        }
    }

    subject = subject + " - " + Calendar.getInstance().getTime();

    try {
        // Create a default MimeMessage object. 
        Message message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(smtpConnection.getUser()));

        // Set To: header field of the header.
        message.setRecipients(Message.RecipientType.TO, iAddresses);

        // Set Subject: header field
        message.setSubject(subject);


        // Create the message part
        BodyPart messageBodyPart = new MimeBodyPart();

        // Now set the actual message
        messageBodyPart.setText(htmlBody);

        // Create a multipart message
        Multipart multipart = new MimeMultipart();

        // Set text message part
        multipart.addBodyPart(messageBodyPart);

        if (filename != null) {
            // Part two is attachment
            messageBodyPart = new MimeBodyPart();

            System.out.println(filename);

            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);
        }

        // Send the complete message parts
        message.setContent(multipart);

        // Send message
        Transport.send(message);

        System.out.println("Sent message successfully...");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

SmtpConnection包含发送电子邮件的所有必要配置。在我的笔记本电脑上,它工作正常,但是当我将这个罐子传递给另一个人时,它只是在java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25, timeout -1;

之后哭泣并消失

但是smtpConnection对象正确提供了主机,端口,用户和密码。

我在这里缺少什么?

0 个答案:

没有答案