• Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaMail Code Suggestions  RSS feed

 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same topic has been posted on StackOverFlow. Need inputs from fellow Code Ranchers too.
http://stackoverflow.com/questions/25545226/javamail-code-suggestions

I am writing a Bean which can send same message to multiple user or can send different message to same user. My application is REST based and an email needs to be triggered when a service is hit.Application can send any number of emails in a day. I have written below class for the same.Any improvements that I can do in below code. I fetch values for properties like from ,host,etc from my Db.I am querying the Db only once and populating javax.mail. Session object with the same.

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kinds of suggestions are you looking for? What is not working as you would like it to?
 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to know if I can implement the above code in a more elegant way.
Any re-factoring that can be done.

Should I be making the class a singleton?
Most of the variables in this class will remain constant except TO-Address ,Subject and Body.
Any other way by which I can make sure that all variables except (TO-Address ,Subject and Body) gets instantiated only once.

Regards,
Ankur
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How you intend to use this class determines its design to a good deal - we can't really comment on that.

I guess my question would be why you're not using a library like http://commons.apache.org/proper/commons-email/ for this?
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agree with Ulf, why to reinvent the wheel.

for code re factoring, one quick thing is to refactor sendEmailToUserList and sendEmailToUser should be refactored since they have 90% similar code. same goes with createEmailMessage and createBulkEmailMessage
 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am facing some issue while using commons-mail API.

Getting following Exception .
Caused by: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
at javax.mail.Session.getProvider(Session.java:237)
at javax.mail.Session.getTransport(Session.java:346)
at javax.mail.Session.getTransport(Session.java:376)
at javax.mail.Transport.send(Transport.java:67)
at javax.mail.Transport.send(Transport.java:48)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
... 3 more


Below is snippet of Code that I am using


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Commons Email is built on top of JavaMail, so it needs all its libraries in the classpath - it seems that's not the case.
 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have added mail-1.4.jar in my class path and it works.
Is there any issue if i keep geronimo-javamail_1.4_spec-1.7.1.jar and geronimo-activation_1.1_spec-1.1.jar also in my classpath?

Regards,
Ankur

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what those are. Check what classes they contain, and make sure not to have overlaps with other jar files.
 
Ankur Luthra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is geronimo*activation*.jar contains the package javax.activation and I see javax.activation as a dependency for commons-email.
hence the question.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JAF (a.k.a Java Activation Framework) can be downloaded from Oracle if you want/need the original jar file.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!