【问题标题】:how to handle proxy's when dealing with email server?处理电子邮件服务器时如何处理代理?
【发布时间】:2013-10-15 07:12:35
【问题描述】:

我已经实现了电子邮件类并从属性文件中获取所有属性。 这是我的代码:

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 代理阻止了我的服务器。如何克服这个问题

【问题讨论】:

  • 尝试在端口 465 上使用 TLS。由于垃圾邮件发送者,端口 25 经常被阻止。
  • 您确定它是代理而不是普通防火墙吗?

标签: java email proxy smtp


【解决方案1】:

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

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

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

确认有效后,您可能希望将设置放入您的 /mail.properties 文件中:

mail.smtp.port:587

【讨论】:

    猜你喜欢
    • 2015-05-20
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多