I tried that send mail in Java.
My customer used contact form.
My Customer hope that change "mail from". It is "user email" that sent from "contact form". (This is spoofing mail. But, no problem. Because send from customer server to customer mail server.)
But, I have no idea that set envelope from in java.
I tried java code below.
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 = "from@gmail.com";
String password = "password";
String contentType = "text/plain";
String subject = "Test Subject";
String from = "from@gmail.com";
String to = "to@gmail.com";
String bounceAddr = "from@gmail.com";
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();
}
I think it is usually code.
But, I got error.
503 not your domain (#5.5.1)
Probably set "mail from" that not register email address from customer mail server.
So, I tried telnet too that use terminal.
$ printf "%s\0%s\0%s" from@gmail.com from@gmail.com 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:<from@gmail.com> // my type
250 ok
RCPT TO:<to@gmail.com> // my type
250 ok
DATA // my type
354 go ahead
subject:Test Mail // my type
from:User Mail <spoof@gmail.com> // my type. set envelope from mail address.
to:to@gmail.com // 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.
This is successed.
How to set envelope from in java that like case telnet above.
Thank you for any help you can provide.
Thank you for comments. I was solved that problem.
I was cording that mail server login id string at mail.smtp.from
.
And, I was written that spoofing string at from
of message.addFrom(InternetAddress.parse(from));
.
props.put("mail.smtp.from", ADMINI_MAIL_ADDRESS); // mail server login id.
and
message.addFrom(InternetAddress.parse(from)); // spoofing mail address string.
Thank you for everyone!