Connect to SMTP Server using Java

The SMTP protocol is widely used to transmit email from client applications to mail servers. Establishing a reliable connection is the first step before sending messages. This article demonstrates how to connect to an SMTP server programmatically with Java, and also explains how to route the connection through SOCKS or HTTP proxies.

Java API to Connect to SMTP Server

To communicate with an SMTP server we use Aspose.Email for Java, a robust library for building feature‑rich email client applications. Aspose.Email simplifies SMTP connections and supports proxy configurations. You can either download the JAR file or add the library via Maven using the snippets below.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-email</artifactId>
    <version>21.3</version>
    <classifier>jdk16</classifier>
</dependency>

Connect to an SMTP Server using Java

Follow these steps to establish a direct SMTP connection:

  • Create an instance of the SmtpClient class and provide the server host.
  • Configure the username, password, port, and security options on the SmtpClient object.
  • Send the email message.

The code sample below demonstrates a basic SMTP connection in Java.

Use Proxy to Connect to an SMTP Server

Many environments place SMTP servers behind proxy firewalls for security. To reach such servers, you must configure your Java client to use a proxy. Aspose.Email for Java offers built‑in support for both SOCKS and HTTP proxies.

Connect to SMTP Servers via SOCKS Proxy in Java

Aspose.Email for Java supports SOCKS 4, SOCKS 4a, and SOCKS 5 protocols. Use the steps below to configure an SMTP client to connect through a SOCKS proxy.

  • Create an instance of SmtpClient and set the host, username, and password.
  • Instantiate a SocksProxy object with the proxy server address, port, and SOCKS version.
  • Apply the proxy to the client using SmtpClient.setProxy(SocksProxy).
  • Send the email.

The following example shows how to connect to an SMTP server via a SOCKS proxy.

Connect to an SMTP Servers via HTTP Proxy in Java

To route SMTP traffic through an HTTP proxy, follow these steps:

  • Create an HttpProxy instance with the proxy host and port.
  • Instantiate an SmtpClient and configure host, username, password, and port.
  • Set the proxy on the client using SmtpClient.setProxy(HttpProxy).
  • Send the email.

The code sample below illustrates an SMTP connection using an HTTP proxy.

Get a Free API License

Obtain a free temporary license to use Aspose.Email for Java without the standard evaluation restrictions.

Conclusion

This guide showed how to programmatically connect to an SMTP server with Java and how to configure both SOCKS and HTTP proxies using Aspose.Email. For deeper details, refer to the official documentation or ask questions on our forum.

See Also