• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to set Return-Path to an email address other than Sender address using JavaMail?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just started using Javamail V 1.4. I would like to implement the following functionality:
Send an email FROM a mail address but redirect the bounce back emails to another mail address that is different from the FROM address.
Eg: Send an email with FROM address as abc@123.com but set the 'Return-Path' to another email address, say xyz@123.com.

I am using message.setHeader("Return-Path", "xyz@123.com") to accomplish this. But it does not seem to work. Could you please help me with the right code to be used?

Thank you.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try setting the Reply-To address instead. javax.mail.Message has a special method for it.
 
smee bond
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the following piece of code and it works fine:

props.put("mail.smtp.from", "xyz@123.com");
Session session = Session.getDefaultInstance( props, null );
MimeMessage message = new MimeMessage( session );
message.addFrom(InternetAddress.parse("abc@123.com"));

I went through Javamail API and found that if the property "mail.smtp.from" is not set, then the FROM address is selected by default as return address. If the property is set to another mail id, all the bounce backs are redirected to the other id!
This works perfectly fine....Thanks to the suggestion provided at the following link:
https://coderanch.com/t/273916/java/java/set-return-path-MimeMessage

But, I have another question now:
Is it possible to set 'mail.smtp.from' property to more than one mail addresses so that bounce backs are redirected to multiple addresses?
If so, how can I do this?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try just providing a list of addresses separated by commas?
 
smee bond
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the following options and none of them worked!

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hasn't anyone read my post? Because this is exactly where the Reply-To address is for - and you can set an entire array of addresses too.
 
smee bond
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for overlooking your message!

Actually I tried using
void javax.mail.internet.MimeMessage.setReplyTo(Address[] arg0).



But it doesnot redirect the bounce back emails to xyz@123.com.
Instead when I click on 'Reply' button in the mail I receive, the Reply-To is set to this address(xyz@123.com) rather than the FROM address (abc@123.com).
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, you want bounce messages to be returned as well. Never knew that reply-to wouldn't work for those (never tested either).

If you set the Return-Path header, does it show up in the email after it is sent (and not bounced)?
 
smee bond
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I set Return-Path Header, it does not show up in the email after it is sent
Only setting the property "mail.smtp.from" works fine to redirect the bounce backs emails to another address.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

smee bond wrote:Is it possible to set 'mail.smtp.from' property to more than one mail addresses so that bounce backs are redirected to multiple addresses?



Hi smee bond,
Mine case also same as yours, that is bounce backs need to be redirected to multiple addresses. In fact, I too tried few things like you and none of them worked.

Did you manage to find any solution for this?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This stackoverflow post explains that you need to set the sender's from using MimeMessage.addFrom() (not MimeMessage.setFrom() ) and that you need to set the "mail.smtp.host".

Basically, it is up to the server to set the Return-Path header.

The test program below will send bounced messages to an email address different from the sender's email address and only to that special bounced message email address.

HTH



 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic