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

标签 java proxy smtp

我通过 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" );
  }

}

这工作得很好,但是如果我在代理上下文中使用相同的方法, 我总是收到错误:

535 身份验证凭据无效

-为什么会发生这种情况? - 如何解决电子邮件发送问题?

最佳答案

您应该阅读this FAQ answer

JavaMail does not currently support accessing mail servers through a web proxy server. 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. The firewall will typically block most access to the Internet, but will allow requests from the proxy server to pass through. 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.

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. Similar properties exist for the "imap" and "pop3" protocols.

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. See the Networking Properties guide for the latest documentation of the socksProxyHost and socksProxyPort properties. These are system-level properties, not JavaMail session properties. 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. Note that setting these properties directs all TCP sockets to the SOCKS proxy, which may have negative impact on other aspects of your application.

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. JavaMail does not support direct access through an HTTP proxy web server.

关于java - SMTP - 通过代理发送电子邮件失败 (JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38077509/

相关文章:

java - 从 Integer java 列表的列表中查找第一个元素

java - 代理服务器的 Axis2 NTLM 身份验证

Python请求+代理+302重定向,为什么请求不正确?

c++ - 使用 C++ WINSOCK(TCP、SSL)发送电子邮件

email - Mandrill电子邮件未到达任何邮箱,但Mandrill显示状态 'Delivered'

Java AppDomain 之类的抽象?

java - JFrame 未出现

c# - CefSharp 3 在运行时设置代理

c# - SmtpClient - 发送到不同的域

java - NoClassDefFoundError 使用来自 Java 的 Scala 类