我尝试用 Java 发送邮件。
- 操作系统
- 使用版本 1.7
我的客户使用了联系表。
我的客户希望更改“邮件来自”。 这是从“联系表格”发送的“用户电子邮件”。 (这是欺骗邮件。但是,没问题。因为从客户服务器发送到客户邮件服务器。)
但是,我不知道在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 = "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();
}
我认为这通常是代码。
但是,我遇到了错误。
503 not your domain (#5.5.1)
可能设置了“邮件来自”,而不是从客户邮件服务器注册电子邮件地址。
所以,我也尝试了使用终端的 telnet。
$ 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.
成功了。
如何在java中设置信封,就像上面的telnet案例一样。
感谢您提供的任何帮助。
最佳答案
感谢您的评论。我已经解决了这个问题。
我正在将邮件服务器登录 ID 字符串编码为 mail.smtp.from
。
并且,我在 message.addFrom(InternetAddress.parse(from));
的 from
处写入了该欺骗字符串。
props.put("mail.smtp.from", ADMINI_MAIL_ADDRESS); // mail server login id.
和
message.addFrom(InternetAddress.parse(from)); // spoofing mail address string.
谢谢大家!
关于java - 如何在Java中设置 "mail from"和 "envelope from"(版本1.7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41154945/