0
Javaコードから電子メールを送信しようとしています。私はjavax.mailライブラリを使用しています。 これは私のコードです:javax.mail指定された送信元アドレスがメールの送信に使用されていません
public class SendMail {
public void postMail(final String recipients, final String subject, final String message) throws MessagingException {
boolean debug = false;
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "gmail-smtp.l.google.com");
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
try {
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress("[email protected]");
msg.setFrom(addressFrom);
InternetAddress addressTo = new InternetAddress(recipients);
msg.setRecipient(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(message, "text/html");
Transport.send(msg);
} catch (MessagingException e) {
System.out.println("SendMail:postMail - " + e.getMessage() + "; " + e.getCause());
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "password");
}
}
}
すべてがうまくworskが、受信者がメッセージをreciveする場合、送信者は私が指定したものが、私は認証に使用するものではありません。この場合、送信者はmessage.setFrom命令で使用した「[email protected]」ではなく「[email protected]」です。
何が間違っているか知っていますか?
を参照してください、あなたは同じことを可能にするSMTPサーバーを使用する必要があります。 Googleでは、認証を使用して、存在しないメールIDを使用して電子メールを送信しないようにしています。したがって、送信者[email protected]を設定しても、元のメールIDが使用されます。 – Logan
申し訳ありません、多分私はプロバイダを変更します。ありがとう@Logan – vilandra