I am sending Bulk Emails using Java API, and I would Like To have a Report of all Bounced Emails. Reading Around I found that I can Implement a Bounced Emails Address:
props.put("mail.smtp.from", bounceAddr);
But the Emails always get Bounced back to the Original Address. Here is My relevant piece of Code:
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();
The Emails always get Bounced back to mysourceaddress@gmail.com. Is there something I am Missing or are there other Better ways to Implement this?