aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes How to set Return-Path to an email address other than Sender address using JavaMail? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "How to set Return-Path to an email address other than Sender address using JavaMail?" Watch "How to set Return-Path to an email address other than Sender address using JavaMail?" New topic
Author

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

smee bond
Ranch Hand

Joined: Sep 29, 2007
Posts: 34
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Try setting the Reply-To address instead. javax.mail.Message has a special method for it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
smee bond
Ranch Hand

Joined: Sep 29, 2007
Posts: 34
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:
http://www.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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Did you try just providing a list of addresses separated by commas?
smee bond
Ranch Hand

Joined: Sep 29, 2007
Posts: 34
I tried the following options and none of them worked!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

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

Joined: Sep 29, 2007
Posts: 34
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

Joined: Oct 27, 2005
Posts: 19723
    
  20

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

Joined: Sep 29, 2007
Posts: 34
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.
Gnanakumar Thavamani
Greenhorn

Joined: Oct 18, 2010
Posts: 1
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?
George Impagliazo
Ranch Hand

Joined: Sep 25, 2010
Posts: 41

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



 
wood burning stoves
 
subject: How to set Return-Path to an email address other than Sender address using JavaMail?