如何在处理电子邮件服务器时处理代理?

3
我已经实现了邮件类并从属性文件中提取了所有属性。 以下是我的代码:
static {
        // Load the properties file
        try {
            properties = new Properties();
            InputStream inputStream = Email.class.getClassLoader()
                    .getResourceAsStream("/mail.properties");
            properties.load(inputStream);`enter code here`
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * 
     * @param to
     *            : mail Sent to
     * @param from
     *            : mail sent from
     * @param subject
     *            : mail's subject
     * @param body
     *            : mail's body
     * @throws Exception
     */
    public static void sendTextMail(String to, String from, String subject,
            String body) throws Exception {

        if (properties.isEmpty()) {
            throw new Exception("Cannot  send mail. Host  data not available.");
        }

        // Authenticate the session with username and password
        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication((String) properties
                        .get("mail.login.username"), (String) properties
                        .get("mail.login.password"));
            }
        });

        // Create to and from addresses
        InternetAddress fromAddress = new InternetAddress(from);
        InternetAddress toAddress = new InternetAddress(to);

        // Create the message instance
        // and add the sender, recipient, subject and body.
        Message msg = new MimeMessage(session);
        msg.setFrom(fromAddress);
        msg.setSubject(subject);
        msg.setRecipient(RecipientType.TO, toAddress);
        msg.setContent(body, "text/plain");

        // Finally send the email
        Transport.send(msg);

    }

当我尝试发送邮件时,出现了以下错误: 16:48:23,882 错误 AuthorController:1199 - 无法连接到SMTP主机:smtp.gmail.com,端口:25 代理服务器正在阻止我的服务器。如何解决这个问题?

1
尝试在端口465上使用TLS。由于垃圾邮件发送者的存在,端口25经常被阻止。 - artbristol
1
你确定它是代理而不仅仅是普通的防火墙吗? - Ian McLaird
1个回答

0

大多数ISP会阻止通过25端口的外部连接。但是,通常可以使用587端口进行非加密SMTP通信。

要更改使用的端口,您可以调整在静态块中设置的Properties实例:

...
properties.load(inputStream);
properties.put("mail.smtp.port", "587");
...

确认程序可行后,您可能希望将设置放在您的/mail.properties文件中:

mail.smtp.port:587

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接