Java で SMTP サービスクライアントを使用したメッセージ送信

SmtpClient から参照してください com.aspose.email.services.inetmail.client パッケージは SMTP サーバー経由でメッセージを送信します。クラスパスにメールプロトコルライブラリが必要です。詳細は メールプロトコルライブラリの設定.

テキストメッセージの送信

でメッセージを作成します InetMessage.builder()を作成し、次にそれを send。送信者と少なくとも 1 つの受信者を設定してください。受信者がいないメッセージの場合は、 send 例外を投げます MailClientException サーバーに接続する前に実行されます。送信者が設定されていないメッセージは受信者に届きますが From アドレスを書き込み、クライアントは警告をログに記録します。

import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.SmtpClient;
import com.aspose.email.services.inetmail.model.InetMessage;

try (SmtpClient smtp = SmtpClient.builder()
        .host("smtp.example.com")
        .port(587)
        .starttls()
        .credentials(new PasswordCredentials("user@example.com", "password"))
        .build()) {
    InetMessage message = InetMessage.builder()
            .from("user@example.com")
            .to("user@example.com")
            .subject("Weekly report")
            .textBody("The report is ready.")
            .build();
    smtp.send(message);
}

各呼び出しは send はサーバーへの独自接続を開き、メッセージ送信後に閉じます。

認証なしでの接続

たとえばローカルネットワークのリレーホストのように、認証なしでメッセージを受け付けるサーバーがあります。そのようなサーバーに接続する場合は、 credentials:

import com.aspose.email.services.inetmail.client.SmtpClient;
import com.aspose.email.services.inetmail.model.InetMessage;

try (SmtpClient smtp = SmtpClient.builder()
        .host("relay.example.com")
        .port(25)
        .noSecurity()
        .build()) {
    smtp.send(InetMessage.builder()
            .from("user@example.com")
            .to("user@example.com")
            .subject("Weekly report")
            .textBody("The report is ready.")
            .build());
}

ローカルホスト名の設定

クライアントが接続すると、ローカルコンピュータ名をサーバーに送ります。この名前を取得するため、メールライブラリは DNS でローカルアドレスを検索します。逆引き DNS レコードが無い場合、検索に数秒かかり、毎回の呼び出しで繰り返されます: send. ルックアップをスキップするには、名前を localHost:

import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.SmtpClient;

SmtpClient smtp = SmtpClient.builder()
        .host("smtp.example.com")
        .credentials(new PasswordCredentials("user@example.com", "password"))
        .localHost("client.example.com")
        .build();

受信者と送信者名の設定

この to, cc および bcc メソッドは 1 つ以上のアドレスを受け取ります。送信者の表示名を示すには、最初に名前、次にアドレスを渡します:

import com.aspose.email.services.inetmail.model.InetMessage;

InetMessage message = InetMessage.builder()
        .from("Reports Team", "user@example.com")
        .to("user@example.com", "user2@example.com")
        .cc("user3@example.com")
        .bcc("user4@example.com")
        .subject("Weekly report")
        .textBody("The report is ready.")
        .build();
smtp.send(message);

HTML メッセージの送信

使用する htmlBody メッセージの HTML バージョンを設定します。さらに設定すると textBody、メッセージは両方のバージョンを含み、受信者側のメールクライアントはサポートする方を表示します:

import com.aspose.email.services.inetmail.model.InetMessage;

InetMessage message = InetMessage.builder()
        .from("user@example.com")
        .to("user@example.com")
        .subject("Weekly report")
        .textBody("The report is ready.")
        .htmlBody("<p>The report is <b>ready</b>.</p>")
        .build();
smtp.send(message);

添付ファイルの追加

添付ファイルを追加するには、 InetAttachment ファイル名、コンテンツタイプ、データを持つオブジェクトを作成し、次にそれを attach:

import com.aspose.email.services.inetmail.model.InetAttachment;
import com.aspose.email.services.inetmail.model.InetMessage;
import java.nio.charset.StandardCharsets;

byte[] data = "Total: 42".getBytes(StandardCharsets.UTF_8);

InetMessage message = InetMessage.builder()
        .from("user@example.com")
        .to("user@example.com")
        .subject("Weekly report")
        .textBody("The report is attached.")
        .attach(InetAttachment.of("report.txt", "text/plain", data))
        .build();
smtp.send(message);

エラー処理

メッセージが送信できない場合(たとえばサーバーが認証情報を拒否したとき) send 例外を投げます MailClientException, これに拡張されます java.io.IOException:

import com.aspose.email.services.inetmail.MailClientException;
import com.aspose.email.services.inetmail.model.InetMessage;

try {
    smtp.send(InetMessage.builder()
            .from("user@example.com")
            .to("not a valid address")
            .subject("Weekly report")
            .textBody("The report is ready.")
            .build());
} catch (MailClientException e) {
    System.out.println(e.getMessage() + ": " + e.getCause().getMessage());
}