首页 > 解决方案 > 我可以使用 Java 代码从 Windows Server 2012 RS 发送邮件吗?

问题描述

我最初尝试编写代码以从 localhost 发送邮件,但我不确定如何设置服务器。浏览了有关此的帮助主题,并没有太大帮助。

是否可以编写 Java 代码从 Windows Server 2012 发送邮件?

如果没有,请帮我在我的 Windows Server 2012 中设置 localhost。我迷路了。

这是我使用的代码:

public static void main(String[] args) throws AddressException, MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.host", "10.204.10.116"); 
        props.put("mail.smtp.port", "443");
        props.put("mail.debug", "true");
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("abc@gmail.com"));
        message.setRecipient(RecipientType.TO, new InternetAddress("xyz@gmail.com"));
        message.setSubject("Notification");
        message.setText("Successful!", "UTF-8"); // as "text/plain"
        message.setSentDate(new Date());
        Transport.send(message);        
    }
}

这是我得到的错误:

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: 10.204.10.116, port: 443;
nested exception is:
java.net.ConnectException: Connection timed out: connect

不知道我应该如何处理这个错误。

标签: javajakarta-mail

解决方案


推荐阅读