1

I have a class say TestEmail which has a sendEmail() method

Now the sendEmail method calls an apache commons method which internally call methods from the javax.mail package. I want to override one of the internal methods. I am providing the code flow below:

TestEmail.sendEmail()->
commons.Email.send()->commons.Email.sendMIMEMessage()->
javax.mail.Transport.send()->javax.mail.Transport(){saveChanges()}-> 
javax.mail.MimeMessage.updateHeaders()->updateMessageID()

I want to override the updateMessageID method. within my TestEmail class. Is it possible? How?

Abhiroop Sarkar
  • 2,251
  • 1
  • 26
  • 45
  • Not sure it's possible, unless you also override all upper methods to call the newer versions. – Mordechai Jun 03 '15 at 10:22
  • Unless ― of course ― you have acces to their source, so you can change just 2 classes, the one you want to change, and the method _directly_ calling it. – Mordechai Jun 03 '15 at 10:25
  • This is a java internal method. I am looking for a solution without modifying its source code. I am not sure if this helps: http://stackoverflow.com/questions/1387207/can-a-parent-call-child-class-methods – Abhiroop Sarkar Jun 03 '15 at 10:27
  • Well, is the method _calling_ the method you want to override, within your scope? – Mordechai Jun 03 '15 at 10:30
  • This would contradict the *encapsulation principle*. – Willem Van Onsem Jun 03 '15 at 10:37

2 Answers2

0

I think it may be possible by extending three classes: commons.Email, javax.mail.Transport and javax.mail.MimeMessage; and overriding their respective methods to use new behavior of updateMessageID().

AmolB
  • 149
  • 6
  • But what if I don't have the source and I don't have any idea what else is done?( `super` won't help, as it would still call the un-overrided version...) – Mordechai Jun 03 '15 at 17:00
0

Looks like you would just override Email.createMimeMessage(Session) in which ever subclass of Email that you are using. Then have createMimeMessage return a subclass of javax.mail.MimeMessage that overrides updateMessageID to do what you want. If you just need to remove the userid then there are other ways to do that. See: Override Message-ID by configuration for details.

Community
  • 1
  • 1
jmehrens
  • 10,580
  • 1
  • 38
  • 47