Javaでメールを送信しようとしました。Java(バージョン1.7)で "mail from"と "envelope from"を設定する方法
- OS X
- 使用バージョン1.7
私の顧客使用のコンタクトフォーム。
私のお客様は、 "mail from"を変更することを希望します。 「連絡フォーム」から送信されたのは「ユーザーメール」です。 (これは偽装メールですが、問題はありません。顧客サーバーから顧客メールサーバーに送信するためです)
しかし、私はJavaでエンベロープを設定することは考えていません。
私は以下のJavaコードを試しました。
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
private static void sendMail() throws Exception {
String smtpServer = "hogehoge.smtp.server.com";
int port = "587";
String userid = "[email protected]";
String password = "password";
String contentType = "text/plain";
String subject = "Test Subject";
String from = "[email protected]";
String to = "[email protected]";
String bounceAddr = "[email protected]";
String body = "send mail";
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.from", bounceAddr);
Session mailSession = Session.getInstance(props);
mailSession.setDebug(true);
MimeMessage message = new MimeMessage(mailSession);
message.addFrom(InternetAddress.parse(from));
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(subject);
message.setContent(body, contentType);
Transport transport = mailSession.getTransport();
try{
System.out.println("Sending ....");
transport.connect(smtpServer, port, userid, password);
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
System.out.println("Sending done ...");
}
catch(Exception e) {
System.err.println("Error Sending: ");
e.printStackTrace();
}
transport.close();
}
通常はコードだと思います。
しかし、エラーが発生しました。
503ないドメイン(#5.5.1)顧客のメールサーバからメールアドレスを登録していないことを
おそらく設定 "からのメール"。
だから、私もターミナルを使用するように試みました。
$ printf "%s\0%s\0%s" [email protected] [email protected] password | openssl base64 -e | tr -d '\n'; echo
ZnJvbUBnbWFpbC5jb20AZnJvbUBnbWFpbC5jb20AcGFzc3dvcmQ=
$ telnet hogehoge.smtp.server.com 587
Trying xxx.xxx.xxx.xxx...
Connected to hogehoge.smtp.server.com
Escape character is '^]'.
220 smtp.server.com ESMTP
EHLO localhost // my type
250-smtp.server.com
...
250 AUTH LOGIN PLAIN CRAM-MD5
AUTH PLAIN ZnJvbUBnbWFpbC5jb20AZnJvbUBnbWFpbC5jb20AcGFzc3dvcmQ= // my type
235 ok, go ahead (#2.0.0)
MAIL FROM:<[email protected]> // my type
250 ok
RCPT TO:<[email protected]> // my type
250 ok
DATA // my type
354 go ahead
subject:Test Mail // my type
from:User Mail <[email protected]> // my type. set envelope from mail address.
to:[email protected] // my type
// my type
send mail. // my type
. // my type
250 ok 1481706367 qp 12070
quit // my type
221 smtp.server.com
Connection closed by foreign host.
これが成功しました。
上記のようなtelnetのようなJavaのエンベロープをどのように設定するか。
ご協力いただきありがとうございます。
私は 'mail.smtp.from'が封筒' MAIL FROM'であると信じています – Andreas
あなたが使用しているIPアドレス/ホスト名に関するものかもしれません。 telnet実験では、localhostを使用しました。メールサーバは、 '127.0.0.1' /' localhost'からの接続を信頼して、オフホストのIPアドレスよりも送信者を偽装する傾向があります。 –
お試しください。 message.setFrom(新しいInternetAddress( "[email protected]"、 "No Reply"));とmessage.setReplyTo(InternetAddress.parse( "[email protected]")); –