Search code examples
proxyjakarta-mailsocks

javamail ignoring socks proxy


I've setup javamail to run through a socks proxy, but it won't go through it. I tried setting both mail.smtp.socks.host and mail.smtp.socks.port but it keeps going direct to the mail host. I feel like I'm missing something very basic. any help would be appreciated. Here's my properties:

props.setProperty("mail.smtp.host", this.smtpHost);
props.setProperty("mail.smtp.port", Integer.toString(this.smtpPort));
props.setProperty("mail.smtp.user", this.smtpUser);
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.starttls.enable", Boolean.toString(this.smtpTLS));
props.setProperty("mail.debug", "true");

if (this.useSOCKSProxy) {
   logger.info("Using proxy");
   props.setProperty("mail.smtp.socks.host", "127.0.0.1");
   props.setProperty("mail.smtp.socks.port", Integer.toString(1080));
   props.setProperty("mail.smtps.socks.host", this.socksProxyHost);
   props.setProperty("mail.smtps.socks.port", Integer.toString(this.socksProxyPort));
   props.setProperty("proxySet", "true");
   props.setProperty("socksProxyHost", this.socksProxyHost);
   props.setProperty("socksProxyPort", Integer.toString(this.socksProxyPort));
}

 //Session session = Session.getInstance(props, new SmtpAuthenticator(this.smtpUser,this.smtpPassword));
Session session = Session.getDefaultInstance(props,
   new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(smtpUser, smtpPassword);
      }
   });

 //Session session = Session.getInstance(props, null);
session.setDebugOut(System.out);
session.setDebug(true);
 //Transport tr = session.getTransport("smtp");
 //tr.connect();
 //tr.connect(this.smtpHost,this.smtpPort, this.smtpUser, this.smtpPassword);

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(this.smtpFrom));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
msg.setSubject(subject);
msg.setText(msgToSend.toString());
msg.saveChanges();

Transport.send(msg);

Solution

  • Start by fixing these common mistakes.

    If that doesn't solve the problem, post the debugging output. Set the System property "mail.socket.debug" to "true" to get additional debugging output.

    What version of JavaMail are you using? What JDK?

    Unless "props" is coming from System.getProperties(), setting "socksProxyHost" has no effect. And if you do set it as a System property, you don't need the JavaMail socks properties.