SMTP サーバーへの接続

SSL 対応の SMTP サーバーに接続する際に設定すべきプロパティは以下の通りです。

以下の例では、次の方法を示します。

  1. ユーザー名を設定します。
  2. パスワードを設定します。
  3. ポートを設定します。
  4. セキュリティオプションを設定します。

以下のコードスニペットは、SSL対応の SMTP サーバーに接続する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

SmtpClient client = new SmtpClient("smtp.gmail.com");

// Set username, password, port, and security options
client.setUsername("your.email@gmail.com");
client.setPassword("your.password");
client.setPort(587);
client.setSecurityOptions(SecurityOptions.SSLExplicit);

Socks プロキシサーバー経由でサーバーに接続

時々、外部との通信にプロキシサーバーを使用します。そのような場合、メールクライアントはプロキシアドレスを指定しないとインターネットに通信できません。Aspose.Email は SOCKS プロキシプロトコルのバージョン 4、4a、5 をサポートしています。このドキュメントでは、プロキシメールサーバーを使用したメール送信の動作サンプルを提供します。プロキシサーバー経由でメールを送信するには:

  1. 必要な情報、つまりプロキシアドレス、ポート、SOCKSバージョンを使用してプロキシを初期化します。
  2. 初期化 SmtpClient ホストアドレス、ユーザー名、パスワード、その他の設定とともに。
  3. クライアントの Proxy プロパティを次のように設定します。 Proxy 以前に作成したオブジェクト。

以下のコードスニペットは、プロキシサーバー経由でサーバーに接続する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

SmtpClient client = new SmtpClient("smtp.domain.com", "username", "password");
client.setSecurityOptions(SecurityOptions.SSLImplicit);
String proxyAddress = "192.168.203.142"; // proxy address
int proxyPort = 1080; // proxy port
SocksProxy proxy = new SocksProxy(proxyAddress, proxyPort, SocksVersion.SocksV5);
client.setProxy(proxy);
client.send(new MailMessage("sender@domain.com", "receiver@domain.com", "Sending Email via proxy",
        "Implement socks proxy protocol for versions 4, 4a, 5 (only Username/Password authentication)"));

HTTPプロキシサーバー経由でサーバーに接続

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

HttpProxy proxy = new HttpProxy("18.222.124.59", 8080);
try (SmtpClient client = new SmtpClient("host", 587, "username", "password")) {
    client.setProxy(proxy);
    client.send(new MailMessage("sender@domain.com", "receiver@domain.com", "Sending Email via proxy",
            "Implement socks proxy protocol for versions 4, 4a, 5 (only Username/Password authentication)"));
}

認証メカニズムのカスタマイズ

SMTPサーバーがサポートする認証メカニズムのリストを取得するには、 getSupportedAuthentication メソッド( SmtpClient クラス。このメソッドにより、クライアントはサーバーとの安全な接続を確立するために利用可能な認証方法を判断できます。その後、 setAllowedAuthentication ユーザーが許可する認証タイプの列挙を取得(または設定)するメソッドで、クライアント‑サーバー間の通信に最適な認証メカニズムを選択できます。これにより、メールクライアントの認証方法を明示的に設定できます。

以下のコードサンプルは、メールクライアントの認証をカスタマイズする方法を示しています。

smtpClient.setAllowedAuthentication(SmtpKnownAuthenticationType.Login);

CRAM-MD5認証を使用してサーバーに接続

SMTPサーバーとの安全な認証と通信を確保するために、SMTPクライアントの許可された認証方法としてCRAM-MD5の使用を指定し、強制できます。以下のコードスニペットは、許可された認証タイプを設定する方法を示しています。 SmtpClient:

smtpClient.setAllowedAuthentication(SmtpKnownAuthenticationType.CramMD5);

ホスト上の特定IPアドレスにSMTPクライアントをバインド

メール送信クライアントをホスト上の特定のポートにバインドしてメールを送信する必要が生じる場合があります。これは、Aspose.Email API を使用しても実現できます。 SmtpClient bindIPEndPoint プロパティ。API SmtpClient 特定のIPエンドポイントを指定することで、ホスト上の特定のIPアドレスを使用するように設定できます。以下のコードスニペットは、ホスト上の特定のIPアドレスにSMTPクライアントをバインドする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

SmtpClient client = new SmtpClient("smtp.domain.com", // host
        587, // port
        "username", // username
        "password", // password
        SecurityOptions.Auto // Security Options
);
try {
    client.bindIPEndPoint(new BindIPEndPointHandler() {
        public InetSocketAddress invoke(InetSocketAddress remoteEndPoint) {
            return new InetSocketAddress(0);
        }
    });
    client.noop();
} finally {
    client.dispose();
}

メール送信せずにメールサーバーの資格情報を検証する

メールを送信せずに資格情報を確認する必要がある場合があります。Aspose.Email は validateCredentials() この操作を実行するメソッドです。検証が成功すると、if 文内のコードが実行され、通常はさらなる操作や IMAP サーバーからのデータ取得に使用されます。以下のコードスニペットは、メールを送信せずに資格情報を検証する例を示しています。

try (SmtpClient smtpClient = new SmtpClient(
        server.SmtpUrl, server.SmtpPort, "username", "password", SecurityOptions.Auto)) {
    smtpClient.setTimeout(4000);

    if (smtpClient.validateCredentials()) {
        // to do something
    }
}

メール操作のタイムアウト設定方法

メール操作は、ネットワーク遅延、データサイズ、サーバー性能など多数の要因により時間がかかります。すべてのメール操作にタイムアウトを設定できます。以下のコード例は、次のものを使用してその方法を示しています。 タイムアウト プロパティ。注意:アプリケーションで長時間待たされるのを防ぐために大きな値は設定しないでください。

try (SmtpClient smtpClient = new SmtpClient("host", 587, "username", "password", SecurityOptions.SSLExplicit))
{
    smtpClient.setTimeout(60000); // 60 seconds

    // some code...
}