在JavaAPI中处理跳转的电子邮件
我正在使用JavaAPI发送大量电子邮件,我希望有一份所有被退回电子邮件的报告。通过阅读,我发现我可以实现一个反弹电子邮件地址:在JavaAPI中处理跳转的电子邮件,java,email,jakarta-mail,Java,Email,Jakarta Mail,我正在使用JavaAPI发送大量电子邮件,我希望有一份所有被退回电子邮件的报告。通过阅读,我发现我可以实现一个反弹电子邮件地址: props.put("mail.smtp.from", bounceAddr); 但是这些邮件总是被退回到原来的地址。以下是我的相关代码: String smtpServer = "smtp.gmail.com"; int port = 587; final String userid = "myuserid";//Source Email
props.put("mail.smtp.from", bounceAddr);
但是这些邮件总是被退回到原来的地址。以下是我的相关代码:
String smtpServer = "smtp.gmail.com";
int port = 587;
final String userid = "myuserid";//Source Email
final String password = "mypassword";//Source Email
String contentType = "text/html";
String subject = "test: bounce an email to a different address "
+ "from the sender";
String from = "mysourceaddress@gmail.com";
String to = "myfakeaddress@gmail.com";//some invalid address
String bounceAddr = "mybouncedEmailsaddress@gmail.com";//bounced Emails address
String body = "Test: get message to bounce to a separate email address";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", "587");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.from", bounceAddr);
Session mailSession = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userid, password);
}
});
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 (MessagingException e) {
System.err.println("Error Sending: " + e.getMessage());
}
transport.close();
电子邮件总是被退回到mysourceaddress@gmail.com. 我是否遗漏了什么,或者是否有其他更好的方法来实现这一点?可能重复给出的答案与我给出的帖子有什么不同。这是一样的。我无法知道这一点,因为您没有显示如何创建/配置MIME消息。因此,这是我的完整代码,它仍然会返回到原始邮件。是的,看起来是正确的。看起来像是GMail SMTP问题: