2

I'm using the Spring java mailer class to send email messages to my users: org.springframework.mail.javamail.JavaMailSenderImpl version 1.4 using Spring framework 3.0.7.RELEASE.

I want to set the bounce back message for a failed email to go to my user's email address that doesn't have the same domain as my smtp server. Does anyone know a way to accomplish this? For example: My system sends an email to email-does-not-exist@gmail.com. My smtp server is configured to have a domain somebusiness.com. Upon failure, send the bounceback to my user: test.user@gmail.com.

I read the following article several times: Specifying the bounce-back address for email

I tried to use their method of setting the mail.smtp.from property but it won't send any emails at all (not even counting bounceback attempts from invalid emails yet).

Properties p = new Properties();
p.put("mail.smtp.from", "test.user@gmail.com"); //If I comment this out, it sends emails again
mailSender.setJavaMailProperties(p);
Session session = Session.getDefaultInstance(p, null);
MimeMessage mimeMessage = new MimeMessage(session);

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,
                false, "utf-8");
mimeMessage.setContent(emailBody, "text/html");
helper.setTo(toAddress);
helper.setSubject(subject);
helper.setFrom(fromAddress);
mailSender.send(mimeMessage);

Anyone have an idea of why? The obvious answer seems like the smtp server we are using is blocking it but I was hoping for potential other ideas.

Community
  • 1
  • 1
abeauchamp
  • 766
  • 1
  • 15
  • 28

2 Answers2

0

I'm having a similar problem. I don't have a solution yet, but at the moment I am considering replacing Spring's mail package with org.apache.commons.mail because it has a simple setBounceAddress(emailAddressString) method.

See the very end section "Handling Bounced Messages" of the user guide:

http://commons.apache.org/proper/commons-email//userguide.html

And the API docs:

http://commons.apache.org/proper/commons-email//apidocs/org/apache/commons/mail/Email.html#setBounceAddress(java.lang.String)

Phil
  • 508
  • 4
  • 4
0

I just checked how the Apache Commons Mail implements it's bounce functionality and it actually just sets the from address. It means that you can do the same in Spring Mail by setFrom(...) on a org.springframework.mail.javamail.MimeMessageHelper class.

Source code snippet from org.apache.commons.mail.Email class:

if (this.bounceAddress != null) {
    properties.setProperty(MAIL_SMTP_FROM, this.bounceAddress);
}

See it in the sources: http://grepcode.com/file/repo1.maven.org/maven2/org.apache.commons/commons-email/1.2/org/apache/commons/mail/Email.java#539

Roman
  • 107
  • 1
  • 1
  • 7
  • Calling the `setFrom(...)` method of `MimeMessageHelper` is setting the from-property of a specific message. That is not the same as defining the Java mail property `mail.smtp.from`! But in the snippet you posted this property is set. The Definition of `mail.smtp.from`is `Email address to use for SMTP MAIL command. This sets the envelope return address. Defaults to msg.getFrom() or InternetAddress.getLocalAddress(). NOTE: mail.smtp.user was previously used for this.` – Fencer Mar 07 '18 at 14:59