简体   繁体   English

SMTP-电子邮件通过代理发送失败(JAVA)

[英]SMTP - Email sending fails via proxy (JAVA)

I am sending Emails via SMTP (using the mail provider web.de), using this code: 我正在使用以下代码通过SMTP(使用邮件提供商web.de)发送电子邮件:

    package form.controll;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class NotificationControllerTest {

  @Before
  public void setUp() throws Exception {
  }

  @After
  public void tearDown() throws Exception {
  }

  @Test
  public void test() {
    NotificationController aNotifi = new NotificationController( "smtp.web.de", "587" );
    aNotifi.sendEmail( "TARGETEMAIL@blabla.de", "SENDERSEMAIL@web.de", "THESENDERSPASSWORD", "New User Registration Notification", "New User has just been registered to blabla" );
  }

}

this works just fine, but if I use the same method in a PROXY context, I always get the error: 这工作得很好,但是如果我在PROXY上下文中使用相同的方法,我总是会收到错误:

535 Authentication credentials invalid 535身份验证凭据无效

-Why does this happen? -为什么会这样? -How to fix the email sending? -如何修复电子邮件发送?

You should read this FAQ answer 您应该阅读此常见问题解答

JavaMail does not currently support accessing mail servers through a web proxy server. JavaMail当前不支持通过Web代理服务器访问邮件服务器。 One of the major reasons for using a proxy server is to allow HTTP requests from within a corporate network to pass through a corporate firewall. 使用代理服务器的主要原因之一是允许来自公司网络内的HTTP请求通过公司防火墙。 The firewall will typically block most access to the Internet, but will allow requests from the proxy server to pass through. 防火墙通常会阻止大多数对Internet的访问,但会允许来自代理服务器的请求通过。 In addition, a mail server inside the corporate network will perform a similar function for email, accepting messages via SMTP and forwarding them to their ultimate destination on the Internet, and accepting incoming messages and sending them to the appropriate internal mail server. 此外,公司网络内部的邮件服务器将执行类似的电子邮件功能,通过SMTP接收邮件并将其转发到Internet上的最终目的地,并接受传入邮件并将其发送到适当的内部邮件服务器。

If your proxy server supports the SOCKS V4 or V5 protocol ( http://www.socks.nec.com/aboutsocks.html , RFC1928) and allows anonymous connections, and you're using JDK 1.5 or newer and JavaMail 1.4.5 or newer, you can configure a SOCKS proxy on a per-session, per-protocol basis by setting the "mail.smtp.socks.host" property as described in the javadocs for the com.sun.mail.smtp package. 如果您的代理服务器支持SOCKS V4或V5协议( http://www.socks.nec.com/aboutsocks.html,RFC1928 )并允许匿名连接,并且您使用的是JDK 1.5或更高版本以及JavaMail 1.4.5或较新的版本,您可以按照com.sun.mail.smtp软件包的javadocs中的说明设置“ mail.smtp.socks.host”属性,以每个会话,每个协议为基础配置SOCKS代理。 Similar properties exist for the "imap" and "pop3" protocols. 对于“ imap”和“ pop3”协议,存在类似的属性。

If you're using older versions of the JDK or JavaMail, you can tell the Java runtime to direct all TCP socket connections to the SOCKS server. 如果您使用的是JDK或JavaMail的旧版本,则可以告诉Java运行时将所有TCP套接字连接定向到SOCKS服务器。 See the Networking Properties guide for the latest documentation of the socksProxyHost and socksProxyPort properties. 有关socksProxyHost和socksProxyPort属性的最新文档,请参见网络属性指南。 These are system-level properties, not JavaMail session properties. 这些是系统级属性,而不是JavaMail会话属性。 They can be set from the command line when the application is invoked, for example: java -DsocksProxyHost=myproxy .... This facility can be used to direct the SMTP, IMAP, and POP3 communication from JavaMail to the SOCKS proxy server. 可以在调用应用程序时从命令行设置它们,例如:java -DsocksProxyHost = myproxy...。此功能可用于将SMTP,IMAP和POP3通信从JavaMail定向到SOCKS代理服务器。 Note that setting these properties directs all TCP sockets to the SOCKS proxy, which may have negative impact on other aspects of your application. 请注意,设置这些属性会将所有TCP套接字定向到SOCKS代理,这可能会对应用程序的其他方面产生负面影响。

Without such a SOCKS server, if you want to use JavaMail to access mail servers outside the firewall indirectly, you might be able to use a program such as Corkscrew or connect to tunnel TCP connections through an HTTP proxy server. 如果没有这样的SOCKS服务器,如果要使用JavaMail间接访问防火墙外部的邮件服务器,则可以使用Corkscrew之类的程序,或者通过HTTP代理服务器连接隧道TCP连接。 JavaMail does not support direct access through an HTTP proxy web server. JavaMail不支持通过HTTP代理Web服务器直接访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM