abiieez
abiieez

Reputation: 3189

Configure Return Path with Gmail and Java

I have a working emailer service which normally send out email through a default address, lets say [email protected]

Now I am trying to add a return-path in the email so whenever I receive an email I can directly reply to the sender email. Here's how I configure the properties:

private void sendOut() {
        props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.from", "[email protected]");
        setJavaMailProperties(props);
        Message message = new MimeMessage(getSession());
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("[email protected]"));
        message.setSubject("subject");
        message.setText("content");
        Transport.send(message);

    }

However after sending out the email, I still see that the email is being sent out from my own email [email protected]. I added the mail.smtp.from based on the answer here How to set the Return-Path to an email address other than Sender address using JavaMail?. What did I miss here ?

Upvotes: 0

Views: 1424

Answers (1)

frankieta
frankieta

Reputation: 1012

It looks like from the same answer that you linked that such a service must be permitted from the server side on the smtp server to work.

It's the SMTP server who in last instance will write the Return Path header in the message that is being sent and decide at which address the replies will be sent.

  1. I tried the same approach explained in the answer (setting props.put("mail.smtp.from", "[email protected]")) without success on several SMTP clients.

  2. I tried using the SMTPMessage instead of a MimeMessage as explained in another answer:

    SMTPMessage message = new SMTPMessage(session); message.setEnvelopeFrom("[email protected]"); ... transport.sendMessage(message, message.getAllRecipients());

This is what my smtp server responded me:

EHLO frankieta
250-smtpcmd04.ad.aruba.it hello frankieta, pleased to meet you
250-HELP
250-AUTH LOGIN PLAIN
250-SIZE 524288000
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-STARTTLS
250 OK
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "SIZE", arg "524288000"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "OK", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 2.1.0 <[email protected]> sender ok
RCPT TO:<[email protected]>
250 2.1.5 <[email protected]> recipient ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354 enter mail, end with "." on a line by itself
From: [email protected]
To: [email protected]

The mail received from [email protected] showed this headers:

Return-Path: <[email protected]>
Delivered-To: [email protected]

Received: from frankieta by smtp.server.com with bizsmtp
From: [email protected]
To: [email protected]

So even though the return-path was correctly set at [email protected] in the received mail, trying to reply to this message would take [email protected] as the recipient.

The envelope from will instead be usually used to bounce back error mails (wrong recipent for example). If you try to send a mail to a wrong recipient it will bounce back to the [email protected] address. So I guess it's something in the hands of the smtp server.

I hope that I helped you.

Upvotes: 3

Related Questions