事前構成済みの Postfix サーバーを使用しており、JavaMail を使用して対話しています。メール通知を要求する外部ドメインにメール通知を送信しようとしています。
テスト メールを送信する Java メソッドは次のとおりです。
public void TestEmail() {
String to = "to@domain.com";
String from = "noreply@hostdomain.com";
final String username = "user";
final String password = "password";
String host = "xxx.xxx.xxx.xxx";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("Test Email");
message.setContent("This is a test email", "text/html");
Transport.send(message);
System.out.println("The test message was sent!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
JavaMail コードを含むメソッドを実行すると、
generalError: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 454 4.7.1 <to@domain.com>: Relay access denied
を使用して Postfix サーバーからの送信メールをテストすることができ
echo "This is the body" | mail -s "This is the subject" to@domain.com
、テスト用の受信トレイでメールを受信しました。
Postfix サーバーのすべての設定が 100% わかっているわけではありませんが、そこに問題があるのではないかと疑っていますが、掘り下げてすべてを勝手に変更するほど詳しくはありません。
編集:Authenticator()
セッションの作成からセットアップ
を削除し、Transport.send()
推奨されるコード ブロック に置き換えました
Transport transport = session.getTransport("smtp");
transport.connect(host, 587, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
新しいコード セット全体は次のとおりです。
String host = "xxx.xxx.xxx.xxx";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, null);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("Test Email");
message.setContent("This is a test email", "text/html");
Transport transport = session.getTransport("smtp");
transport.connect(host, 587, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("The test message was sent!");
} catch (MessagingException e) {
System.out.println("Error: " + e);
e.printStackTrace();
throw new RuntimeException(e);
}
}
これにより、受け取ったエラー メッセージが次のように変更されました。Unrecognized SSL message, plaintext connection?
解決済み
解決策: Java コードは問題ではありませんでした。Postfix サーバーは、非ローカル IP/ホストから発信された電子メールを受け入れるように構成されていませんでした。IP は mynetworks 変数の main.cf に追加されました。