How do I specify the reply-to address of a message?

John Zukowski

The setReplyTo() method of the Message class allows to you specify a different set of reply-to addresses than the from field. The following demonstrates. Be sure to provide the method with an array of InternetAddress objects:

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class ReplyExample {
  public static void main (String args[]) 
      throws Exception {
    String host = args[0];
    String from = args[1];
    String to = args[2];
    String reply = args[3];

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("mail.smtp.host", host);

    // Get session
    Session session = Session.getInstance(props, null);

    // Define message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, 
      new InternetAddress(to));
    message.setReplyTo(new InternetAddress[] 
      {new InternetAddress(reply)});
    message.setSubject("Hello JavaMail");
    message.setText("Welcome to JavaMail");

    // Send message
    Transport.send(message);
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact