【问题标题】:JavaMail with proxy not connecting带有代理的 JavaMail 未连接
【发布时间】:2016-08-23 12:20:09
【问题描述】:

我有一个名为“NotifiationService”的类: @Service("notificacionService") 公共类 NotificacionServiceImpl 实现 NotificacionService{

//servicio llama a repositorio
@Autowired
@Qualifier("notificacionService")
private NotificacionService notificacionService;

    @Override
    public void send(String to, String subject, String text) {

        //proxy
        Properties p = System.getProperties();
        p.setProperty("proxySet","true");
        p.setProperty("socksProxyHost","MYPROXY");
        p.setProperty("socksProxyPort","MYPORT");

        Properties props = new Properties();

        // smtp.gmail.com
        props.setProperty("mail.smtp.host", "smtp.gmail.com");

        // TLS
        props.setProperty("mail.smtp.starttls.enable", "false");

        // port
        props.setProperty("mail.smtp.port","465");

        //
        props.setProperty("mail.smtp.user", "reciever@gmail.com");


        props.setProperty("mail.smtp.auth", "true");



        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);

        MimeMessage message = new MimeMessage(session);

        try {

            message.setFrom(new InternetAddress("sender@gmail.com"));


            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));


            message.setSubject(subject);
            message.setText(text);

            //
            Transport t = session.getTransport("smtp");
            t.connect("sender@gmail.com","senderpassword");
            t.sendMessage(message,message.getAllRecipients());
            t.close();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

如您所见,我已尝试配置代理,因为计算机已连接到重定向流量的代理。因此,即使添加有关代理的所有规范,它仍然给我一个错误提示:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.SocketException: Permission denied: connect

我也尝试了不同的端口,例如:25,485,587 并且没有响应,所以我认为这是代理的问题。

为了能够找到有关已实现代理的信息,我在控制台中输入了以下命令:

reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" | find /i "proxyserver"

它会回复:

ProxyServer    REG_SZ    MYPROXY:MYPORT

如果我在 cmd 中输入:“ping google.com”,则表示无法访问

有没有一种方法可以从 java 与 javamail 连接到 gmail 并能够使用当前配置发送电子邮件?

谢谢。

【问题讨论】:

    标签: java proxy connection jakarta-mail ports


    【解决方案1】:

    如果在设置 SOCKS 属性后仍无法正常工作,很可能您的代理服务器只是 Web 代理服务器,而不是 SOCKS 代理服务器。 JavaMail FAQ 具有指向其他软件的指针,这些软件将允许通过网络代理服务器建立隧道连接。

    【讨论】:

      【解决方案2】:

      从 JavaMail 1.6.2 开始,您可以为 Session 对象设置代理身份验证属性以发送电子邮件。

      请参阅以下文档链接。 https://javaee.github.io/javamail/docs/api/

      以下属性是新引入的,可与代理身份验证(基本)配合使用。

      mail.smtp.proxy.host

      mail.smtp.proxy.port

      mail.smtp.proxy.user

      mail.smtp.proxy.password

      【讨论】:

        【解决方案3】:

        至少澄清一点关于 https.proxySet=true 和 http.proxySet=true 的使用。

        JDK-4632974proxySet=false 被 HttpURLConnection.getOutputStream 忽略

        “不再使用属性 http.proxySet 以支持简单的测试 对于 http.proxyHost 属性的存在。该属性存在至 JDK1.0.2,但在 1996 年为 JDK1.1 删除。”

        换句话说,不要使用 proxySet,因为它没有任何用处。它是当今不存在的 Java 系统属性。

        【讨论】:

          猜你喜欢
          • 2013-04-27
          • 1970-01-01
          • 2021-10-21
          • 2015-05-13
          • 2011-03-05
          • 1970-01-01
          • 2017-01-15
          • 1970-01-01
          • 2013-04-21
          相关资源
          最近更新 更多